tags:

views:

80

answers:

2

Is there a way to be notified when a ViewController is removed from a UINavigationController because the back button was pressed?

+2  A: 

You can use viewWillDisappear: in the view controller that is disappearing. If the other view controller needs to be notified, you can use a delegate method to notify it:

//in the disappearing view controller, class MYViewController
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //do stuff you need to do
    if ([self.delegate respondsToSelector:@selector(myViewControllerDidDisappear:)]) 
        [self.delegate myViewControllerDidDisappear:self];  //bottom view controller is delegate
}
eman
viewWillDisappear: also gets called if you push another view controller on top of it, not just when going back.
progrmr
+1  A: 

In conjunction with eman's method, check

[navController.viewcontrollers count]

If it is one greater than before (you need to maintain a count) then something was pushed. If it is one less, and viewWillDisappear: was called, then the view controller was removed.

Adam Eberbach
Thanks, I'll use them both
synic