Checking if a UIViewController is about to get Popped from a navigation stack?

I don't think there is an explicit message for this, but you could subclass the UINavigationController and override - popViewControllerAnimated (although I haven't tried this before myself).

I don't think there is an explicit message for this, but you could subclass the UINavigationController and override - popViewControllerAnimated (although I haven't tried this before myself). Alternatively, if there are no other references to the view controller, could you add to its - dealloc?

The dealloc will only be called after the pop, though, not before. – Jesse Rusak Mar 13 '09 at 12:03 I don't think that's the best solution. I want to use this controller in other places in the app, and the behaviour I want to execute is specific to this controller and has to happen when the controller is popped.

I don't want to have to subclass every navController this viewController appears in. – Jasarien Mar 13 '09 at 13:10 It seems that this is best solution. It seems to work, so thanks!

– Jasarien Mar 13 '09 at 13:46 1 Try this: subclass UIViewController, override popViewController:animated: and send a custom message to the UIViewController's delegate. Then, the delegate can decide what it needs to do in each case. – Alex Mar 13 '09 at 20:42 1 Subclassing 'UINavigationController' will lead app to be rejected by apple.

Documentation – HelmiB Mar 13 '097 at 8:00.

You can catch it here. (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated; if (viewController == YourAboutToAppearController) { do something } This will fire just before the display of the new View. Nobody's moved yet.

I use all the time to do magic in front of the asinine NavigationController. You can set titles and button titles and do whatever there.

My experimentation suggests that actually UINavigationController visibleViewController is already set to YourAboutToAppearController. Though indeed the animation has yet to start. – Max Howell Jan 11 at 16:41.

I tried this: - (void) viewWillDisappear:(BOOL)animated { // If we are disappearing because we were removed from navigation stack if (self. NavigationController == nil) { // YOUR CODE HERE } super viewWillDisappear:animated; } The idea is that at popping, the view controller's navigationController is set to nil. So if the view was to disappear, and it longer has a navigationController, I concluded it was popped.(might not work in other scenarios).

Can't vouch that viewWillDisappear will be called upon popping, as it is not mentioned in the docs. I tried it when the view was top view, and below top view - and it worked in both. Good luck, Oded.

1 An interesting idea and approach, but I fear it may be slightly too fragile. It relies on an implementation detail that could change at any time. – Jasarien Mar 31 at 11:25 Agreed, hence that last skepticism.

– Oded Ben Dov Apr 3 at 14:18 Thanks Oded, that little snippet helped quite alot! – Reflog May 23 at 7:09.

This is working for me. - (void)viewDidDisappear:(BOOL)animated { if (self. ParentViewController == nil) { NSLog(@"viewDidDisappear doesn't have parent so it's been popped"); //release stuff here } else { NSLog(@"PersonViewController view just hidden"); } }.

This is exactly what I needed. Thanks. – grahamparks Sep 1 '10 at 11:18 This doesn't work with 4.1.– chrish Oct 22 '10 at 13:40.

I have the same problem. I tried with viewDisDisappear, but I don't have the function get called :( (don't know why, maybe because all my VC is UITableViewController). The suggestion of Alex works fine but it fails if your Navigation controller is displayed under the More tab.In this case, all VCs of your nav controllers have the navigationController as UIMoreNavigationController, not the navigation controller you have subclassed, so you will not be notified by the nav when a VC is about to popped.

Finaly, I solved the problem with a category of UINavigationController, just rewrite - (UIViewController *)popViewControllerAnimated:(BOOL)animated - (UIViewController *)popViewControllerAnimated:(BOOL)animated{ NSLog(@"UINavigationController(Magic)"); UIViewController *vc = self. TopViewController; if (vc respondsToSelector:@selector(viewControllerWillBePoped)) { vc performSelector:@selector(viewControllerWillBePoped); } NSArray *vcs = self. ViewControllers; UIViewController *vcc = vcs objectAtIndex:vcs count - 2; self popToViewController:vcc animated:YES; return vcc;} It works well for me :D.

This is a great solution and not fragile at all as other suggestions. One could also use a Notification so anyone wanting to know about popped views could listen in. – David H Nov 2 at 19:49.

Maybe you could use UINavigationBarDelegate's navigationBar:shouldPopItem protocol method.

1 I tried that first. However, my Navigation Bar is managed by the navigation controller, and manually setting the delegate of the bar to be my view controller results in an exception that explains manually setting the delegate on the nav bar is not allowed if the bar is managed by a nav controller. – Jasarien Mar 13 '09 at 20:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions