-1

How can I make a Method display a new ViewController? I am trying to make the

Currently what I have :

-(void)gameOver{

    [groundMovement invalidate];
    [guyMovement invalidate];

    topGround.hidden = YES;
    bottomGround.hidden = YES;
    guy.hidden = YES;
    scoreLabel.hidden = YES;


    gameViewController *viewController = [[UIStoryboard storyboardWithName:@"gameOver" bundle:nil] instantiateViewControllerWithIdentifier:@"gameOverViewController"];

    [self presentViewController:viewController animated:YES completion:nil];

    if (score > highScore) {

        [[NSUserDefaults standardUserDefaults] setInteger:score forKey:@"HighScoreSaved"];
    }        
}

enter image description here

enter image description here

Nightmare
  • 85
  • 9

2 Answers2

1
-(void)terminate{

    MyNewViewController *myNewVC = [[MyNewViewController alloc] init];

    // do any setup you need for myNewVC

    [self presentModalViewController:myNewVC animated:YES];
}
pkatsourakis
  • 1,024
  • 7
  • 20
0

Since your are using storyboards in your project, first make sure that your view controllers have a storyboard identifier. You can set it through the identity inspector in xCode. Then, you can instantiate a new view controller by calling:

YourViewControllerClass *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewControllerId"]; // Replace "MainStoryboard" and "ViewControllerId" with your storyboard name and the storyboard id for your view controller respectively

Then, depending on how your needs you can either push the new view controller (if for example you're using a navigation controller):

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

or present it modally

[self presentViewController:viewController animated:YES completion:nil];
spassas
  • 4,778
  • 2
  • 31
  • 39
  • Would I put : `YourViewControllerClass *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];` in the method and also include `[self presentViewController:viewController animated:YES];` @spassas – Nightmare Aug 05 '14 at 16:03
  • @Sephireth Yes, the first line instantiates your VC, the second presents it. – spassas Aug 05 '14 at 16:06
  • I get an error on the second line :No visible @interface for 'gameViewController' declares the selector 'presentViewController:animated:' – Nightmare Aug 05 '14 at 16:07
  • @Sephireth Sorry, my mistake. Try `[self presentViewController:viewController animated:YES completion:nil];`. I edited the answer as well – spassas Aug 05 '14 at 16:11
  • Now when its executed in-game it crashes the app. I am not sure if I setup Storyboard IDs correctly, I have 3 VC , do I give each the same SB ID or different ones. ? – Nightmare Aug 05 '14 at 16:14
  • @Sephireth Make sure that your storyboard name, as well as view controller storyboard id and class are set properly. What error do you get? – spassas Aug 05 '14 at 16:16
  • terminating with uncaught exception of type NSException – Nightmare Aug 05 '14 at 16:17
  • Check [this question](http://stackoverflow.com/questions/13867565/what-is-a-storyboard-id-and-how-can-i-use-this) for understanding the storyboard IDs – spassas Aug 05 '14 at 16:21
  • @Sephireth Your identifier is `gameOver` and not `gameOverViewController`. And your storyboard name is wrong, replace it with the filename of your storyboard file without the `.storyboard` extension – spassas Aug 05 '14 at 16:38