IOS how to detect programmatically when top view controller is popped

iosstackuinavigationcontroller

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.

Best Answer

iOS 5 introduced two new methods to handle exactly this type of situation. What you're looking for is -[UIViewController isMovingToParentViewController]. From the docs:

isMovingToParentViewController

Returns a Boolean value that indicates that the view controller is in the process of being added to a parent.

- (BOOL)isMovingToParentViewController

Return Value
YES if the view controller is appearing because it was added as a child of a container view controller, otherwise NO.

Discussion
This method returns YES only when called from inside the following methods:

-viewWillAppear:
-viewDidAppear:

In your case you could implement -viewWillAppear: like so:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.isMovingToParentViewController == NO)
    {
        // we're already on the navigation stack
        // another controller must have been popped off
    }
}

EDIT: There's a subtle semantic difference to consider hereā€”are you interested in the fact that VC2 in particular popped off the stack, or do you want to be notified each time VC1 is revealed as a result of any controller popping? In the former case, delegation is a better solution. A straight-up weak reference to VC1 could also work if you never intend on reusing VC2.

EDIT 2: I made the example more explicit by inverting the logic and not returning early.