xcode issues when coming from another view control

2019-07-16 14:22发布

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.

3条回答
可以哭但决不认输i
2楼-- · 2019-07-16 14:43

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:

// in your application delegate's application:didFinishLaunchingWithOptions: method

UINavigationController *nav = [UINavigationController alloc] initWithRootViewController:home];
nav.navigationBarHidden = YES; // this hides the nav bar
self.window.rootViewController = nav;

// from your 'home' VC to present the game vc:

GameViewController *game = [[GameViewController alloc] initWithNibName:nil bundle:Nil];
[self.navigationController pushViewController:game animated:YES];

// from your 'game' VC to get back to 'home':

[self.navigationController popViewControllerAnimated:YES];

You're only instantiating 'home' and 'game' view controllers in one place, and UINavigationController presents the view controller at the top of its stack.

查看更多
做个烂人
3楼-- · 2019-07-16 14:49

You should look for Segues instead ! You could not dismiss the source viewcontroler.

查看更多
闹够了就滚
4楼-- · 2019-07-16 14:59

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:

GameViewController *game = [[GameViewController alloc] initWithNibName:nil bundle:Nil];
[self presentViewController:game animated:YES completion:NULL];

game -> home

[self dismissViewControllerAnimated:YES completion:NULL];
查看更多
登录 后发表回答