Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone XCode:单个ViewController转到多个ViewController_Iphone_Ios_Objective C_Xcode_Viewcontroller - Fatal编程技术网

Iphone XCode:单个ViewController转到多个ViewController

Iphone XCode:单个ViewController转到多个ViewController,iphone,ios,objective-c,xcode,viewcontroller,Iphone,Ios,Objective C,Xcode,Viewcontroller,我看到过很多主题,其中多个ViewController指向一个ViewController,但不是相反 我正在制作一个游戏,您可以从game_select.m中选择一个游戏,它需要转到6个视图控制器中的一个。我尝试过使用故事板和硬编码,但都不适合我 我已经将Game1.h和Game2.h导入Game_select.m。 当我运行代码时,它总是转到Game1 ViewController。 这是我正在尝试的代码: if(getGame1) { //go to game1 G

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

我正在制作一个游戏,您可以从game_select.m中选择一个游戏,它需要转到6个视图控制器中的一个。我尝试过使用故事板和硬编码,但都不适合我

我已经将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];
    }
感谢您在advanced中的帮助。
干杯。

我个人不会在这种情况下使用导航控制器。将所有ViewController子类设置为(普通)
UIViewController
,然后使用此代码显示其中一个ViewController:

请注意,此代码仅在以编程方式设置视图或使用xib(非故事板)时有效,如果使用
initWithNibName:bundle:
而不是使用
init

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];
}

就我个人而言,我不会在这种情况下使用导航控制器。将所有ViewController子类设置为(普通)
UIViewController
,然后使用此代码显示其中一个ViewController:

请注意,此代码仅在以编程方式设置视图或使用xib(非故事板)时有效,如果使用
initWithNibName:bundle:
而不是使用
init

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];
}

仔细检查
getGame1
getGame2
是否为
BOOL
值,而不是某种类型的对象,如果其非nil,则会计算为YES。我犯过很多次这样的错误:

NSNumber* booleanValue = @NO;
if (booleanValue) {
    // this code will run
}

if ([booleanValue boolValue]) {
    // this code will not run - as expected
}

说真的,我已经犯了很多次这个错误。

再次检查
getGame1
getGame2
是否为
BOOL
值,而不是某种如果其非nil将计算为YES的对象。我犯过很多次这样的错误:

NSNumber* booleanValue = @NO;
if (booleanValue) {
    // this code will run
}

if ([booleanValue boolValue]) {
    // this code will not run - as expected
}

说真的,我犯了很多次这个错误。

创建从主viewController到其他viewController的手动分段。首先,单击要显示的viewController。确保已选择viewController本身,而不是其中一个视图:

然后单击segues选项卡(最右侧),并从“手动Segue”圆圈拖动到要从中分离的viewController

然后单击序列并在选项卡中为其指定不同的名称,如下所示:

[self performSegueWithIdentifier:@"showAlternate" sender:nil];

然后,在您的代码中,您将有这样一行:

[self performSegueWithIdentifier:@"showAlternate" sender:nil];

您可以使用它来显示“showAlternate”标识符的viewController。您将有多个标识为“Game1”和“Game2”的分段。

创建从主viewController到其他viewController的手动分段。首先,单击要显示的viewController。确保已选中viewController本身,而不是其中一个视图:

然后单击segues选项卡(最右侧),并从“手动Segue”圆圈拖动到要从中分离的viewController

然后单击序列并在选项卡中为其指定不同的名称,如下所示:

[self performSegueWithIdentifier:@"showAlternate" sender:nil];

然后,在您的代码中,您将有这样一行:

[self performSegueWithIdentifier:@"showAlternate" sender:nil];


您可以使用它来显示“showAlternate”标识符的viewController。您将有多个带有“Game1”和Game2等标识符的序列。

您正在使用Game1来实例Game2。你的问题中有错吗?或者这是您的代码中的错误。是的,抱歉,已修复。这看起来是正确的,可以将视图控制器推到堆栈上。您是否检查过
getGame1
是否为空值?@Anthony如果您的意思是,当Game2为真时,我是否确定Game1为假,那么是的。那么您的意思是,无论您做什么,都会显示
Game1
视图?如果您已经完成了代码,并且确定执行了
Game2*Game2=[[Game2 alloc]init]…
,我不知道该说什么。可能
Game2
被不正确地定义为
Game1
?您正在使用Game1来实例Game2。你的问题中有错吗?或者这是您的代码中的错误。是的,抱歉,已修复。这看起来是正确的,可以将视图控制器推到堆栈上。您是否检查过
getGame1
是否为空值?@Anthony如果您的意思是,当Game2为真时,我是否确定Game1为假,那么是的。那么您的意思是,无论您做什么,都会显示
Game1
视图?如果您已经完成了代码,并且确定执行了
Game2*Game2=[[Game2 alloc]init]…
,我不知道该说什么。也许
Game2
在某个地方被不正确地定义为
Game1
?另外,让我指出,如果getGame1和getGame2都为真,您将遇到问题。当我尝试此操作时,屏幕会变黑。您是在使用xibs还是以编程方式排列视图?在这种情况下,您需要使用segues。此代码不起作用的原因(可能还有您的原始代码)是因为您没有告诉Game1或Game2它应该使用故事板文件。我将更新我的答案。看看我的第二个答案。另外,让我指出,如果getGame1和getGame2都为真,您将遇到问题。当我尝试此操作时,屏幕变黑。您是使用xibs还是以编程方式排列视图?在这种情况下,您需要使用segues。此代码不起作用的原因(可能还有您的原始代码)是因为您没有告诉Game1或Game2它应该使用故事板文件。我将更新我的答案。看一下我的第二个答案。我不认为我遇到了这个问题,但以防万一,谢谢,我很感激。我不认为我遇到了这个问题,但以防万一,谢谢,我很感激。创建一个segue是当你右击一个对象并将其转到另一个ViewController时,对吗?如果是这样,你想让我