iOS routing viewDidAppear to child view controllers?

pachun picture pachun · Dec 24, 2013 · Viewed 15.8k times · Source

I'm adding a child view controller to a parent view controller, and everything works as expected, except that child view controller isn't having its usual callbacks triggered. eg, things like viewWillAppear(animated) is never called. I looked into it and thought it may be because I wasn't calling willMoveToParentViewController and didMoveToParentViewController on the child view controller before and after adding it to the parent, respectively. Unfortunately, fixing that has made no difference. Does anyone know how I can wire this up so that when I do addChildViewController and removeChildViewController the regular callbacks are triggered? After adding the childViewController, I also add its view as a subview to the parent view controller's view. At neither point (addChildViewController & addSubview) does the child view controller's viewWillAppear(animated), etc methods get called...

Nick

Answer

VChemezov picture VChemezov · Dec 25, 2013

viewWillAppear, viewDIdAppear called automatically when you adding your ViewControler view to a view hierarchy

viewWillDisappear, viewDidDisappear called automatically when you removing your ViewControler view from a view hierarchy

Maybe this methods not called because you are adding sub-controller view before displaying main view controller and, therefore your main view is not in view hiererchy itself?

When your main view controller appears or disappears you should call this methods for childs in corresponding methods.