Reinitialize a view controller – iPhone


I have a multi view application with individual UIViewControllers and xibs. I have one view that is the game view and every time I call it I want it to re-initialize, by that I mean restart at it's initial condition and not in the state I last left it in.

Here is the code block I am using to load and remove views:

-(void)loadStartViewController {
[currentView.view removeFromSuperview];
StartViewController *startViewController = [[StartViewController alloc] initWithNibName:@"StartView" bundle:nil];
[window addSubview:startViewController.view];
currentView = startViewController;


I should be more clear here. I have a UIViewController and a xib that make up the view while the game is being played. They call a method in a UIView to programatically draw the view. I leave the UIViewController when the game ends and I go to a score screen. I want to have the user restart and go back into the game, IE load the UIViewController with the above code but start with a new game, all settings are in the method called. I could just set a variable and pass it between UIViewControllers but I though there might be a command that would allow me to reset the game UIViewController so it does not remember anything about the state at the end of the last game.

To make it work I wrote a method to reset all the variables in the UIView that draws the game screen and call that method from the UIViewController in the viewDidLoad section.

Best Solution

I assume your view is displaying some data stored in model (game state). So wouldn't you want to reset the game state and let the view display it? The view is managed (loaded/released/reloaded) by the system when it needs to be displayed or released when memory on phone is low. So if you handle your game state separately you should update the game state (the model) and let the view display it.

What do you mean my "calling the view"? Do you mean when you go from a 'menu' to 'a game screen'?

Maybe re-loading the game state after the view is displayed would help. There are some delegates like viewDidLoad but my understanding is that they are being called after the view is loaded. However, if you're running out of memory your view may be release and reloaded later so I guess you should manage the game 'state' yourself in model object and let the view just display it.

Not sure if that helps, I'm not quite sure what you meant my 'calling the view'.