iphone - XCode:单个 ViewController 转到多个 ViewController

标签 iphone ios objective-c xcode viewcontroller

我看过很多主题,其中多个 ViewController 转到一个 ViewController,但不是相反。

我正在制作一个游戏,您可以从 Game_select.m 中选择一个游戏,它需要发送到 6 个 View Controller 之一。我试过使用 Storyboard并对其进行硬编码,但对我来说都没有用。

我已经将 Game1.h 和 Game2.h 导入到 Game_select.m 中。 当我运行我的代码时,它总是转到 Game1 ViewController。 这是我正在尝试的代码:

    if(getGame1) {
    //go to game1
    Game1 *game1 = [[Game1 alloc] init];
        [self.navigationController pushViewController:game1 animated:YES];
    }

    if(getGame2) {
    //go to game2
    Game2 *game2 = [[Game2 alloc] init];
        [self.navigationController pushViewController:game2 animated:YES];
    }

感谢您的帮助。 干杯。

最佳答案

我个人不会在这种情况下使用导航 Controller 。使您的所有 viewControllers 成为(正常的)UIViewController 的子类,然后使用此代码呈现您的 viewControllers 之一:

请注意,此代码仅在您以编程方式设置 View 或使用 xib(而 Non-Storyboard )时才有效,如果您使用 initWithNibName: bundle: 而不是使用 初始化

if(getGame1) {
    //go to game1
    Game1 *game1 = [[Game1 alloc] init];
    [self presentViewController:game1 animated:YES completion:nil];
}

if(getGame2) {
    //go to game2
    Game2 *game2 = [[Game2 alloc] init];
    [self presentViewController:game2 animated:YES completion:nil];
}

关于iphone - XCode:单个 ViewController 转到多个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16006545/

相关文章:

iphone - UINavigationcontroller 推送和弹出

iphone - 使用什么模型为 iPhone 应用程序提供翻译?

ios - 在 Storyboard 中,UIScrollview 无法正常使用自动调整大小。没有代码如何管理?

iphone - "[context save:&error]"和 "[appDelegate saveContext]"之间的区别

ios - 获取 NSString 的前两个字符

ios - UIImageView 的双色边框

ios - 如何根据其标签值属性在uiimageview上添加图像。?

iphone - 更快的 if 语句

objective-c - b/w addObject :dictionary and addObject:[dictionary copy] to an NSMutableArray? 有什么区别

ios - 如何设置 UIScrollview 和动画的优先级