UINavigationController popViewController is popping everything!

26,307

Solution 1

I am seeing some odd UINavigationController stack behavior with just using

[self.navigationController popViewControllerAnimated:YES];

inside of a delegate called out of a tableView:didSelectRowAtIndexPath: call.

The views don't work right, and a UINavigationController provided "back" operation doesn't correctly pop the view of this delegate, going back another level.

I found that if I used

[self.navigationController popToViewController:self animated:YES];

instead, that suddenly everything worked fine. This is in an application with ARC turned on.
So, I can only guess that there is some reference housekeeping that doesn't happen correctly unless you tell it to pop back to a specific view controller when you will pop a view controller that will immediately become "unreferenced" by that pop.

Solution 2

[[self navigationController] popViewControllerAnimated:NO];
//here you pop **self** from navigation controller. And now 
[self navigationController] == nil;   
// And 
[nil viewControllers] == nil

Try to do this:

UINavigationController *nc = [self navigationController];
NSLog(@"%@", [nc viewControllers]);
[nc popViewControllerAnimated:NO];
NSLog(@"%@", [nc viewControllers]);

Solution 3

I fixed it. The code that was popping the view was getting called in viewDidLoad. This meant it was getting popped before the view had actually animated in completely.

I moved that code to viewDidAppear and now it works as advertised.

Share:
26,307
Nico
Author by

Nico

Author of numerous little Mac apps (and a blog). From April 21, 2014 until August 2018, I worked for Apple, as a Software QA Engineer on the Foundation framework team. Answers and edits before April 21, 2014 were written before I knew anything internal. Anything I posted during that time was only about APIs and behaviors that were public—i.e., verifiable in released software or promised by documentation. Above all, nothing I've written here, or anywhere else public, should be construed as having ever been an opinion or statement from Apple, nor a promise of future behavior or features. Opinions, when I give them, are my own.

Updated on June 13, 2020

Comments

  • Nico
    Nico almost 4 years

    So I'm trying to pop a view controller off the stack when an error occurs, but it seems like it's popping too much off in one go. The navigation bar up the top loses its title and buttons, but the old table view data remains visible. I have no idea what's going on...

    The basic set up is:

    • Tab View template
      • Navigation controller
        • View controller (Loaded from the xib)
        • View controller (Pushed, what I want to pop)

    Here's the code:

    NSLog(@"%@", [[self navigationController] viewControllers]);
    [[self navigationController] popViewControllerAnimated:NO];
    NSLog(@"%@", [[self navigationController] viewControllers]);
    

    The resulting NSLog's show:

    2009-09-22 19:57:14.115 App[34707:550b] (
        <MyViewController: 0xd38a70>,
        <MyViewController: 0xd36b50>
    )
    2009-09-22 19:57:14.115 App[34707:550b] (null)
    

    Anyone have experience with this?

  • Admin
    Admin over 14 years
    Ok, this shows that it is not emptying the whole stack. BUT it still does not have the desired effect. The UITableView remains while the navigation controls disappear (leaving the background gradient however, the "bar")...