views:

73

answers:

1

I'm writing a 'load' feature for my current iPhone app.

Practically what I want to do is get the user back where s/he left off. To do so I want to recreate all the UIViewControllers appeared before.

My problem is the UIViewControllers won't appear if I simply call [[self navigationController] pushViewController:newController animated:YES];.

The same code works fine when invoked as an event handler, like after touching a button. But if I do same without an even (for ex. in viewDidLoad) the new view controller's loadView method won't get called.

My actual code (with some pseudo elements):

- (void)viewDidLoad {
    [super viewDidLoad];
    if (loading)
        [self onSomeEvent];
}

- (void)onSomeEvent {
    UIViewController *newController = //init....;
    [[self navigationController] pushViewController:newController animated:YES];
    [newController release];
}

I guess viewDidLoad is not the right place to do such a call, but then what is?

A: 

I'm not sure that spreading your "load" feature all accross your controllers is the best way to achieve it.

I would rather put it in the init of your application, in the applicationDidFinishLauching part. Then you have a centralized place where you restore the previous state (easier to handle IMHO).

Let's suppose you want to implement some more advanced restore feature like:

  • displaying a splash UIView with an activity indicator to indicate the user that you're restoring previous state
  • restore the stack of your controllers in the navigation controller
  • remove the splash

it's easier to have all this code in the applicationDidFinishLauching : it's all managed at one point.

Morever, when restoring the old state to your navigation controller, you can avoid using the transitions and use animated:NO instead of YES when pushing your controllers. It will be easier to handle from your perspective and the restore time may be decreased if you remove time needed to achieve transitions.

yonel