I need to know when my view controller is about to get popped from a nav stack so I can perform an action.
I can't use -viewWillDisappear, because that gets called when the view controller is moved off screen for ANY reason (like a new view controller being pushed on top).
I specifically need to know when the controller is about to be popped itself.
Any ideas would be awesome, thanks in advance.
Try making this check in viewwilldisappear if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) { //popping of this view has happend. }
Try overriding
willMoveToParentViewController:
(instead ofviewWillDisappear:
) in your custom subclass ofUIViewController
.You can use this one:
Maybe you could use UINavigationBarDelegate's navigationBar:shouldPopItem protocol method.
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?
I tried this:
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.