iOS: How to Recognize that We Got Back from a Child UIViewController within the Parent UIViewController?

Ohad Regev picture Ohad Regev · Aug 2, 2011 · Viewed 9.5k times · Source

Let's say that I have 2 UIViewControllers on a stack within a UINavigationController. In the "parent" we call "[self.navigationController pushViewController:childViewController animated:YES];" upon some user action and in the "child" we call "[self.navigationController popViewControllerAnimated:YES];" upon some user action.

How can we recognize within the parent that we just got back?

Is there some "event" driven method that can recognize that this popViewControllerAnimated action was called from the child?

Answer

MayorJustin picture MayorJustin · Aug 2, 2011

It seems like you're using this child controller as a modal in that it can be 'dismissed'. If that is the case, try to follow Apple's patterns that they use for UIAlertViews.

If that is the case, I'd do either of the following to implement a delegate pattern(delegate vs block is a huge debate that I will not get into here) so the owner(the one that pushes the child on) knows when its dismissed:

  • Create a protocol (ChildControllerDelegate), have one method in it childControllerWasDismissed:(ChildController *)
  • add a block property(make sure its a copy property, not retain) to the ChildController

You'll then want to call the delegate method or block on viewDidDisappear. If you want finer grain control, have a delegate method or block that corresponds viewWillDisappear / viewDidDisappear.