Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to first view controller in app

I need to go to the first view in my app. I have a few views pushed onto the stack then a modal navigation controller and more views pushed onto that.

The problem I'm having is that using [[self navigationController] popToRootViewControllerAnimated:YES]; only goes back to the first view in the modal stack.

And I can't get [[self navigationController] popToViewController:.. to work because the true first view controller isn't accesible with [[self navigationController] viewControllers].

Any ideas on how to accomplish this? Thanks.

like image 709
ScottF Avatar asked Apr 13 '12 20:04

ScottF


1 Answers

Do this:

[[self navigationController] dismissModalViewControllerAnimated:YES];

That will get you back to the VC that modally presented the navigation controller. Getting farther back after that depend on how you pushed those "few views" before the navigation controller.

Edit - explanation to get to the deepest root...

It sounds like those "few views" are on another, underlying navigation controller's stack. This can be a little tricky, because the clean way to get farther back in that stack is to have that underlying navigation controller pop to it's own root. But how can it know that the modal VC on top of it is done?

Let's call the view controller that did the modal presentation of second navigation controller VC_a. It's a modally presented navigation controller whose topmost VC is VC_b. How can VC_a know to pop to it's navigation root when VC_b modally dismisses itself?

The good answer (usually) is that VC_b decided to dismiss itself for a reason - some condition in your app/model changed to make it decide to be done.

We want VC_a to detect this condition, too. When VC_b gets dismissed, and VC_a gets a viewWillAppear message because it's about to be uncovered:

// VC_a.m

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    if (/* some app condition that's true when VC_b is done */) {
        // I must be appearing because VC_b is done, and I'm being uncovered
        // That means I'm done, too.  So pop...
        [self.navigationController popToRootViewControllerAnimated:NO];
    } else {
        // I must be appearing for the normal reason, because I was just pushed onto the stack
    }
}
like image 154
danh Avatar answered Sep 30 '22 15:09

danh