Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 515
Ohad Regev Avatar asked Aug 02 '11 13:08

Ohad Regev


People also ask

When to use child view controller?

Child view controllers are especially useful for UI functionality that we wish to reuse across a project. For example, we might want to display a loading view as we're loading the content for each screen — and that can easily be implemented using a child view controller, that can then simply be added when needed.

What is UIViewController in iOS?

A UIViewController is an object which manages the view hierarchy of the UIKit application. The UIViewController defines the shared behavior and properties for all types of ViewController that are used in the iOS application. The UIViewController class inherits the UIResponder class.

What is a navigation controller?

NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.


1 Answers

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.

like image 180
MayorJustin Avatar answered Oct 05 '22 20:10

MayorJustin