Reinitialize View Controller - iPhone
I have a multiple view app with separate UIViewControllers and xib. I have one view, which is a game view and every time I call it I want it to be reinitialized, by that I mean restarting in this initial state, not the state I was last in left him once.
Here is the code 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 clearer here. I have a UIViewController and an xib that make up a view while playing. They call a method in UIView to programmatically paint a view. I leave the UIViewController when the game ends and I go to the score screen. I want the user to reboot and return to the game, IE loads the UIViewController with the above code, but it starts with a new game, all settings are in the called method. I could just set a variable and pass it between the UIViewControllers, but although there might be a command that will allow me to reset the game UIViewController so that it doesn't remember anything about the state at the end of the last game.
To do this, I wrote a method to reset all the variables in the UIView that draw the game screen and call this method from the UIViewController in the viewDidLoad section.
a source to share
I am assuming your view is displaying some data stored in the model (game state). So, would you like to reset the game state and let it be displayed? View control (loaded / released / reloaded) by the system when it should be displayed or displayed when the memory on the phone is low. Therefore, if you are handling your game state separately, you must update the game state (model) and allow it to be displayed.
How do you feel about the "vision challenge"? Do you mean when you go from "menu" to "game screen"?
Perhaps reloading the game state after showing the view will help. There are delegates like viewDidLoad , but I understand that they are called after the view has loaded. However, if you run out of memory, your view can be released and reloaded later, so I think you should manage the game "state" yourself in the model object and let the view just render it.
Not sure if this helps, I'm not entirely sure what you meant what I call browsing.
a source to share
Instead of keeping track of which view is currently being displayed, use a UINavigationController. What are they needed for. In applicationDidFinishLaunching in the application delegate:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:startViewController];
Create an instance variable in startViewController and pass it a pointer to the navigation controller after it's created. Then when you want to go to the game:
[self.navigationController pushViewController:gameViewController animated:YES];
or not animated, whatever. You probably also want to hide the navigation bar with:
[self.navigationController setNavigationBarHidden:YES animated:NO];
When the game is over, you can keep clicking any views you want and you can always go back to the first view with:
[self.navigationController popToRootViewControllerAnimated:YES];
Oh, and as for reinitializing the game every time you play, just implement
-(void)viewWillAppear:(BOOL)animated{}
in your game view controller and do whatever initialization you want. Though honestly, I'm not 100% sure you should be using this with .xib ... I rarely use Interface Builder, so I'm not sure. But I think it will work.