views:

114

answers:

2

(This is both question and answer since it took quite a bit of digging to find the real answer.)

Symptom: viewWillAppear, viewDidAppear were not being called in my UIViewController.

Cause: Embedding a UINavigationController or UITabBarController (my case) in a UIViewController somehow interrupts with the calling of these methods.

Solution: Call them manually in the UIViewController that contains the aforementioned UINavigationController / UITabBarController.

For example (assuming projectNavigationController is your UINavigationController):

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

-(void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated];
    [projectNavigationController viewWillDisappear:animated];
}

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated];
    [projectNavigationController viewDidAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated];
    [projectNavigationController viewDidDisappear:animated];
}

In my case I had an inner UITabBarController and I called the methods accordingly and all was solved.

(Attribution on the solution: http://davidebenini.it/2009/01/03/viewwillappear-not-being-called-inside-a-uinavigationcontroller/)

A: 

Your fix is a workaround for your bad code. You are not supposed to 'embed' view controllers in other view controllers.

If you do, for example if you were writing your own tabbarcontroller-like view controller, then you should add the views of your 'sub-viewcontroller' to the window and not as subviews of the root uiviewcontroller.

If you add them to the window then the system will actually also send your view controller the right callbacks.

St3fan
+2  A: 
imaginaryboy
Interesting analysis imaginaryboy. I appreciate your POV St3fan.In typical setups, the UITabBarController (which has it's own navigation controllers and view controllers) is added to the window. It's very easy to see this.However, my situation was slightly different. In my case the user enters a specific mode of the application via a modal. In this other mode, there is a different UITabBarController from the one the user sees originally. Perhaps I could've called the app delegate to add the new UITabBarcontroller to the window (?) but adding it to the modal UIViewController was easier.
freshfunk