ios 在没有 prepareForSegue 的情况下推送到另一个视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15371995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:41:18  来源:igfitidea点击:

push to another view controller without prepareForSegue

iosobjective-cuiviewcontrolleruistoryboardseguepushviewcontroller

提问by Marius Behrens

How can you push to another view controller without prepareForSegue?

你怎么能在没有 的情况下推送到另一个视图控制器prepareForSegue

myClassVC *viewController = [myClassVC alloc];
UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] initWithIdentifier:@"pushToMyVC" source:self destination:viewController];

if ([segue.identifier isEqualToString:@"pushToMyVC"]) {
 NSLog(@"logging");
 myClassVC *viewController = (myClassVC *)[segue destinationViewController];
 [self presentViewController:viewController animated:YES completion:nil];        
}

回答by Rob

If you want to programmatically invoke a push segue, you give the segue a "storyboard id" in Interface Builder and then you can:

如果你想以编程方式调用推送转场,你可以在界面生成器中给转场一个“故事板ID”,然后你可以:

[self performSegueWithIdentifier:"pushToMyVC" sender:self];

Alternatively, if you don't want to perform the segue, you can instantiate the destination view controller and then manually push to that view controller. All you need to do is to make sure that the destination view controller has its own "storyboard id" in Interface Builder, then you can:

或者,如果您不想执行 segue,您可以实例化目标视图控制器,然后手动推送到该视图控制器。您需要做的就是确保目标视图控制器在 Interface Builder 中有自己的“storyboard id”,然后您可以:

UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestinationController"];
[self.navigationController pushViewController:controller animated:YES];

You said "push" (and hence I used pushViewControllerabove). If you really meant to "present a modal view controller", then that second line is:

你说“推”(因此我在pushViewController上面使用了)。如果你真的想“呈现一个模态视图控制器”,那么第二行是:

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

As you can see, you don't haveto use prepareForSegueto push to new scene. You only use prepareForSegueif you want to pass information to the destination view controller. Otherwise it is not needed.

正如你所看到的,你不必须使用prepareForSegue推向新的场景。仅prepareForSegue当您想将信息传递给目标视图控制器时才使用。否则就不需要了。

Clearly, if you're not using storyboards (e.g., you're using NIBs), then the process is entirely different. But I assume you're not using NIBs because prepareForSegueis not applicable in that environment. But if you were using NIB, it would be as follows:

显然,如果您不使用故事板(例如,您使用的是 NIB),那么过程就完全不同了。但我假设您没有使用 NIB,因为prepareForSegue它不适用于该环境。但是,如果您使用的是 NIB,则如下所示:

SecondViewController *controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];

回答by Richard Brown

[self presentViewController:viewController animated:YES completion:nil];is not needed, as the segue will push the destination view controller using the transition you selected automatically.

[self presentViewController:viewController animated:YES completion:nil];不需要,因为 segue 将使用您自动选择的过渡推送目标视图控制器。

If you don't want to use the segueprocess you'll need to manually push the view controller with:

如果您不想使用该segue过程,则需要使用以下命令手动推送视图控制器:

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

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

but make sure you remove the segues in the Storyboard first.

但请确保先删除 Storyboard 中的 segue。

回答by Naman Vaishnav

   NSString * storyboardName = @"Main";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewControllerID"];
    [self presentViewController:vc animated:YES completion:nil];

回答by Mig70

In my case all viewControllers are built dynamically. I do not make use of the storyboard for this purpose. Should this be your case you may just instantiate the viewController class you want to open and make a push as in my example below:

就我而言,所有 viewController 都是动态构建的。我不会为此目的使用故事板。如果这是您的情况,您可以实例化您想要打开的 viewController 类并进行推送,如下面的示例所示:

MyViewController* myViewController = [[MyViewController alloc] init];
[self.navigationController pushViewController:myViewController animated:YES];