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

查看:138
本文介绍了使用segues在NavController中嵌入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.

我的问题是在少数情况下,我想提出这个相同的viewController使用模态segue。当我这样做时,viewController没有嵌入到navigationController中。有没有办法用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然后呈现,完全可以在没有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 :

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控制器。从另一个视图控制器,您可以控制拖动到此导航控制器以设置模态 segue。您还可以控制拖动到原始视图控制器,以便在没有导航控制器的情况下设置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在NavController中嵌入UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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