ios 使用 segues 在 NavigationController 中嵌入 UIViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16253631/
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 23:22:22  来源:igfitidea点击:

Embed a UIViewController in a NavigationController using segues

iosuinavigationcontrolleruistoryboarduistoryboardsegue

提问by hatunike

I have a viewController that is usually(most often) accessed by using a push segue. This viewController needs to be embedded in a UINavigationController. So typically, this is no problem. The push segue manages pushing the viewController, and therefore the viewController has it's UINavigationController.

我有一个 viewController,它通常(最经常)通过使用 push segue 来访问。这个 viewController 需要嵌入到 UINavigationController 中。所以通常,这没有问题。push segue 管理推送viewController,因此viewController 有它的UINavigationController。

My issue is that in a few cases, I'd like to present this same exact viewController using a modal segue. When I do this, the viewController is not embedded in a navigationController. Is there a way to do this using segues?

我的问题是,在某些情况下,我想使用模态转场来呈现完全相同的 viewController。当我这样做时,viewController 没有嵌入到 navigationController 中。有没有办法使用 segue 做到这一点?

I know this can be done purely in code without segues by creating a UINavigationController, setting the rootView as the viewController and then presenting that modally. That can be done using code like this :

我知道这可以通过创建 UINavigationController,将 rootView 设置为 viewController,然后以模态方式呈现,而无需继续在代码中完成。这可以使用这样的代码来完成:

MyViewController *viewController = [[MyViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self presentModalViewController:navController animated:YES];

But how do I do this same thing but using Segues?

但是我如何做同样的事情而不是使用 Segues?

Update

更新



For further clarity, here is some code to supplement how I used the accepted answer in the prepareForSegue method.

为了进一步清晰,这里有一些代码来补充我如何在 prepareForSegue 方法中使用已接受的答案。

When Using Modal Segue

使用模态转场时

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue destinationViewController] isEqualToString:@"Modal to MyVC"])
    {
        UINavigationController *nav = [segue destinationViewController];
        MyViewController *vc = [nav topViewController];
        //setup vc
    }
}

When Using Push Segue

使用 Push Segue 时

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue destinationViewController] isEqualToString:@"Push to MyVC"])
    {
        MyViewController *vc = [segue destinationViewController];
        //setup vc
    }
}

回答by vacawama

In your Storyboard, you can embed a ViewController in a Navigation Controller by selecting the View Controller and then picking from the menu at the top Editor->Embed In->Navigation Controller. From another view controller, you control drag to this Navigation controller to set up the modalsegue. You can also control drag to the original View Controller to set up segues to it without the Navigation Controller.

在您的 Storyboard 中,您可以通过选择 View Controller 然后从顶部的菜单中选择,将 ViewController 嵌入到导航控制器中Editor->Embed In->Navigation Controller。从另一个视图控制器,您可以控制拖动到此导航控制器以设置转场modal。您还可以控制拖动到原始视图控制器以在没有导航控制器的情况下设置转场。

Storyboard showing a view controller both embedded in a navigation controller and not

故事板显示了嵌入在导航控制器中的视图控制器,而不是嵌入到导航控制器中的