如何让Method显示一个新的ViewController?

时间:2014-08-05 15:19:51

标签: ios objective-c xcode

如何让Method显示一个新的ViewController?我正在尝试制作

目前我所拥有的:

-(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

2 个答案:

答案 0 :(得分:1)

-(void)terminate{

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

    // do any setup you need for myNewVC

    [self presentModalViewController:myNewVC animated:YES];
}

答案 1 :(得分:0)

由于您在项目中使用故事板,因此首先要确保视图控制器具有故事板标识符。您可以通过xCode中的身份检查器进行设置。然后,您可以通过调用:

来实例化新的视图控制器
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

然后,根据您的需求,您可以推送新的视图控制器(例如,如果您使用导航控制器):

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

或以模态方式呈现

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