Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Removing a presented view controller

On a view controller I have a button that will present another view controller. From the second view controller, I can go to other view controllers, but not necessarily back to the one that got me here. If this is the case, how can I remove the original view controller?

like image 258
Carmichael Avatar asked Aug 14 '13 11:08

Carmichael


2 Answers

Your description is a bit unclear here. There could be 3 different cases here:

  1. Moving through navigation controller hierarchy
  2. Breaking out of navigation controller hierarchy to another view controller
  3. Just adding another view controller to current in navigation controller stack

In first case you can use methods of UINavigationController:

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

and use viewControllers property to navigate through the stack.

Ina second one, if you want to break out the hierarchy to one completely another view controller, then simply do it by:

[[[UIApplication sharedApplication] keyWindow].rootViewController dismissViewControllerAnimated:YES completion:nil];
[[UIApplication sharedApplication] keyWindow].rootViewController = newController;

or even better: add second line in completion block of first line.

Or in third case, if you want only to make one exception, but otherwise stay in navigation controller stack, then use methods:

- (void)addChildViewController:(UIViewController *)childController
- (void)removeFromParentViewController
like image 183
mbpro Avatar answered Nov 10 '22 02:11

mbpro


That depends on how you actually presented the current view controller. If it was modally, then

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

If it was pushed using a navigation controller:

[self.navigationController popViewControllerAnimated:YES];
like image 29
Hermann Klecker Avatar answered Nov 10 '22 01:11

Hermann Klecker