使用 segues 在 NavigationController 中嵌入 UIViewController [英] Embed a UIViewController in a NavigationController using segues

查看:15
本文介绍了使用 segues 在 NavigationController 中嵌入 UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的问题是,在某些情况下,我想使用模态 segue 呈现相同的视图控制器.当我这样做时,viewController 没有嵌入到导航控制器中.有没有办法使用 segues 做到这一点?

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?

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

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 :

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

但是除了使用 Segues 之外,我该如何做同样的事情呢?

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

为了更清楚起见,这里有一些代码来补充我如何在 prepareForSegue 方法中使用接受的答案.

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

使用模态转场时

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

使用 Push Segue 时

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

推荐答案

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

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 modal segue. You can also control drag to the original View Controller to set up segues to it without the Navigation Controller.

这篇关于使用 segues 在 NavigationController 中嵌入 UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆