Suppose I have a nav controller stack with 2 view controllers: VC2 is on top and VC1 is underneath. Is there code I can include in VC1 that will detect that VC2 has just been popped off the stack?
Since I'm trying to detect the popping of VC2 from within the code for VC1 it seems that something like viewWillAppear or viewDidAppear won't work, because those methods fire every time VC1 is displayed, including when it is first pushed on the stack.
EDIT: it seems I was not very clear with my original question. Here's what I'm trying to do: determine when VC1 is being shown due to VC2 being popped off the top of the stack. Here's what I'm NOT trying to do: determine when VC1 is being shown due to being pushed onto the top of the stack. I need some way that will detect the first action but NOT the second action.
Note: I don't particularly care about VC2, it can be any number of other VCs that get popped off the stack, what I do care about is when VC1 becomes the top of the stack again due to some other VC begin popped off the top.
What are you specifically trying to do?
If you're trying to detect that VC1 is about to be shown, this answer should help you. Use UINavigationControllerDelegate.
If you're trying to detect that VC2 is about to be hidden, I would just use the
viewWillDisappear:
of VC2.One way you could approach this would be to declare a delegate protocol for VC2 something like this:
in VC1.h
in VC1.m
in VC2.h
VC2.m
There's a good article on the basics of protocols and delegates here.
isMovingTo/FromParentViewController won't work for pushing and popping into a navigation controller stack.
Here's a reliable way to do it (without using the delegate), but it's probably iOS 7+ only.
In my case, using the delegate would mean having the view controllers' behavior be more tightly coupled with the delegate that owns the nav stack, and I wanted a more standalone solution. This works.
You can add an observer for NSNotification specifically JUST for your VC2.
Now in in your VC2's view will disappear, you can post a notification: