views:

937

answers:

2

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.

+1  A: 

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'.

stefanB
A: 

Instead of keeping track of which view is currently displayed, use a UINavigationController. That's what they're for. In applicationDidFinishLaunching in your app delegate:

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:startViewController];

Create an instance variable in startViewController and pass it a pointer to the navigation controller after you create it. Then when you want to go to the game:

[self.navigationController pushViewController:gameViewController animated:YES];

or not animated, whatever. You'll also probably want to hide the navigation bar with:

[self.navigationController setNavigationBarHidden:YES animated:NO];

When the game ends, you can keep pushing whatever views you want, and you can always go back to the first view with:

[self.navigationController popToRootViewControllerAnimated:YES];

Oh, and as for re-initializing the game everytime you play, just implement

-(void)viewWillAppear:(BOOL)animated{}

in the game view controller and do whatever initialization you want. Though honestly I'm not 100% sure you should use that with a .xib...I rarely use Interface Builder so I'm not sure. But I think it would work.