Iphone – How to have a view controller run updating code when it is brought to the top of the stack of views

cocoa-touchiphone

I have a viewController (Planner) that loads two view controllers (InfoEditor and MonthlyPlan) when the application starts. MonthlyPlan is hidden behind InfoEditor (on load).

So my question is when I exchange InfoEditor for MonthlyPlan (MonthlyPlan gets brought to the top) how can I have data on the MonthlyPlan view be updated. An NSLog in viewDidLoad is being called when the application starts (which makes sense.) NSLogs in viewDidAppear and viewWillAppear aren't doing anything.

Any ideas?

Thanks!

— Adding more details —

I'm creating the view hierarchy myself. A simple viewController that is just loading two other viewControllers. The two child viewControllers are loaded at the same time (on launch of application.) To exchange the two views I'm using this code:

[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];

The exchanging of the views is fine. The part that is missing is just some way of telling the subview, you're in front, update some properties.

Best Answer

There's a lack of details here. How are you "exchanging" the two views?

If you were using a UINavigationController as the container then viewWillAppear/viewDidAppear would be called whenever you push/pop a new viewController. These calls are made by the UINavigationController itself. If you ARE using a UINavigationController then make sure you have the prototypes correct for these functions.

- (void)viewWillAppear:(BOOL)animated

If you are trying to implement a view hierarchy yourself then you may need to make these calls yourself as part of activating/deactivating the views. From the SDK page of viewWillAppear;

If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly.

Update:

With the new details the problem is clear: This is a situation where you must send the disappear/appear messages yourself as suggested by the SDK. These functions are not called automagically when views are directly inserted/removed/changed, they are used by higher-level code (such as UINavigationController) that provides hierarchy support.

If you think about your example of using exchangeSubView then nothing is disappearing, one view just happens to cover the other wholly or partially depending on their regions and opacity.

I would suggest that if you wish to swap views then you really do remove/add as needed, and manually send the viewWillAppear / viewWillDisappear notifications to their controllers.

E.g.

// your top level view controller
-(void) switchActiveView:(UIViewController*)controller animated:(BOOL)animated
{
    UIController* removedController = nil;

    // tell the current controller it'll disappear and remove it
    if (currentController)
    {
        [currentController viewWillDisapear:animated];
        [currentController.view removeFromSuperView];
        removedController = currentController;
    }

    // tell the new controller it'll appear and add its view
    if (controller)
    {
        [controller viewWillAppear:animated];
        [self.view addSubView:controller.view];
        currentController = [controller retain];
    }

    // now tell them they did disappear/appear
    [removedController viewDidDisappear: animated];
    [currentController viewDidAppear: animated];
    [removedController release];
}