views:

1939

answers:

2

Hi, I'm trying to reload a value I set on UIViewController to update each time I transition with this code (coming and going are both UIViewControllers):

    coming.phoneNumber = aPhoneNumber;
    [coming viewWillAppear:YES];
[going viewWillDisappear:YES];
[going.view removeFromSuperview];
[self.window insertSubview: coming.view atIndex:0];
[going viewDidDisappear:YES];
[coming viewDidAppear:YES];

when the phone number value changes in the coming view it never shows up when the view is inserted into the subview. How do I make view show the updated phone number?

A: 

I am assuming phoneNumber is an NSString. You have to remember there is a distinction between something being displayed on the screen and the object backing it. A UITextField showing a particular value is not the same as NSString it got that value from, and when you change the NSString you need to change the UITextField's value if you want it to be displayed.

Unless you have a custom implementation of setPhoneNumber: in coming or are using KVO to observe the value there is no way the view are displaying with the phoneNumber will know to update what is on the screen.

It looks like you are trying to implement UINavigationController or UITabBarController. If at all possible you should just use one of them, rather than trying to handle all the transitioning yourself. They take care of attaching and detaching views, call view(Did/Will)(Appeaer/Disappear), etc.

Louis Gerbarg
+1  A: 

You should get the behavior you want by modifying the UIViewController's attributes only after the UIView it represents has been loaded:

[coming viewWillAppear:YES];
[going viewWillDisappear:YES];
[going.view removeFromSuperview];
[self.window insertSubview:coming.view atIndex:0];
coming.phoneNumber = aPhoneNumber;
[going viewDidDisappear:YES];
[coming viewDidAppear:YES];

In the code above, the coming UIViewController has been loaded, but the UIView it represents is not loaded until it is accessed for the first time by coming.view. This is explained in the documentation for UIViewController:

If you access [the view] property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view.

You can determine if a UIView has been loaded via the -isViewLoaded method of UIViewController.

titaniumdecoy
insertView:atIndex: does not load a view, it adds an already instantiated view to a view hierarchy.
Louis Gerbarg
I did some digging and updated my post as appropriate.
titaniumdecoy