如何使用一个按钮跳转到不同的viewcontrollers?

时间:2014-04-16 23:03:44

标签: ios objective-c uibutton viewcontroller

我正在尝试制作一款游戏,您可以从头开始,#34;等级1",并继续游戏或从您最后一次离开的地方开始,例如。 "等级8"。我希望能够在打开的视图控制器上使用一个按钮"继续当前游戏",从最后一次停止的位置开始。我有一个单独的按钮重新开始。我打算使用NSInteger通过在每个级别完成后向其添加1来跟踪级别。所以我想最终的问题是,我该如何编制"继续当前游戏"按钮直接到正确的viewcontroller,例如。 "等级8",使用NSInteger或更简单的方法?谢谢。

顺便说一句,我使用xcode 5和单个视图模板。

1 个答案:

答案 0 :(得分:2)

当你达到理想水平时使用它

//level you game
int lvlGame = 0;
//name you UIViewController by level
NSString * nameVC = [[NSString alloc] initWithFormat:@"gameLvl%i",lvlGame];

如果使用XIB

UIViewController * viewController = [[UIViewController alloc] initWithNibName:nameVC bundle:nil];

如果使用Storyboard

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController * viewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:nameVC];

最后

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