I am developing an iPhone app and have encountered some issues. I have a main view controller and another one called GameViewController. I have a button in the main view controller to go to the game and a back button in the game to go back to the main view controller. If I go to the game view controller and back to the main, the next time I press the button to go to the gameviewcontroller I find all kinds of glitches. Here is my code for the button to go to the gameViewController:
GameViewController *game = [[GameViewController alloc] initWithNibName:nil bundle:Nil];
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:game animated:YES completion:NULL];
Here is the back button code to go to the main view controller:
ViewController *home = [[ViewController alloc] initWithNibName:nil bundle:nil];
UIViewController *parentViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^
{
[parentViewController presentViewController:home animated:NO completion:nil];
}];
If anyone can help it would be greatly appreciated.
Is there a reason you don't use
UINavigationController
? You can use this even if you want to hide the navigation bar and provide your own navigation UI. It sounds like you have your own back button, which will suffice.With UINavigationController, your code would look more like this:
You're only instantiating 'home' and 'game' view controllers in one place, and UINavigationController presents the view controller at the top of its stack.
You should look for Segues instead ! You could not dismiss the source viewcontroler.
You 'back' code isn't really going back, it's going back and then presenting a new copy of the home view controller.
Then, your 'game' code is dismissing, which will remove the current controller (because you presented it when you went back), and then present the game from it.
This is messy as you will have presented controllers whose presenting controllers have been destroyed. You would be better off using a navigation controller. But, you can correct your current code if you just present and dismiss like:
home -> game:
game -> home