在故事板中创建自定义 UINavigationController 不会实例化视图 [英] Creating a custom UINavigationController in storyboard isn't instantiating the view

查看:30
本文介绍了在故事板中创建自定义 UINavigationController 不会实例化视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController,它是在故事板中创建的,并被分配了一个故事板 ID.这个 UIViewController 在代码的不同地方被实例化,因此从所有不同的位置创建 segues 是没有意义的.

I have a UIViewController which was created in a storyboard and has been assigned a storyboard id. This UIViewController is instantiated in various places in code and therefor it didn't make sense to create segues from all the different locations.

视图控制器需要包含在 UINavigationController 中才能正常运行,因为它使用各种导航栏项并且可以推送额外的视图控制器.

The view controller needs to be contained in a UINavigationController in order to function properly because it uses the various navigation bar items and can push additional view controllers.

视图控制器从代码实例化如下:

The view controller is instantiated from code as follows:

// some where in code that needs to present the custom view controller

UIViewController *viewControllerToPresent = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];

[self.viewController presentViewController: viewControllerToPresent animated:YES];

问题是视图控制器未包含在故事板中预期的导航控制器中.我已经尝试为它包含 UINavigationController 分配故事板 ID,但这导致 prepareForSegue 中现有代码出现问题,该代码需要 CustomViewController

problem is that the view controller isn't contained in a navigation controller as expected from the storyboard. I've tried assigning its containing UINavigationController the storyboard id, but that caused problems in existing code in prepareForSegue which expected an instance of CustomViewController

解决此问题的明显选项是在呈现 CustomViewController 之前添加 UINavigationController 的创建:

The obvious option to fix this would be to add creation of a UINavigationController before presenting the CustomViewController:

UIViewController *viewControllerToPresent = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: viewControllerToPresent];
[self.viewController presentViewController: nav animated:YES];

但这并不能解决问题,因为故事板中的导航项未正确添加.

But this doesn't fix the problem either because the navigation items from the storyboard aren't added correctly.

在我看来更合理的是让 CustomViewController 继承自 UINavigationController 并在故事板中构建整个内容,以便自定义视图控制器包含自己的导航栏.

What to me seems more reasonable, is to have the CustomViewController inherit from UINavigationController and build the whole thing in storyboard such that the custom view controller contains its own navigation bar.

我见过从 UINavigationController 继承的 UIImagePickerController 之类的类,我相信他们试图解决的正是这种情况.

I've seen classes such as UIImagePickerController which inherit from UINavigationController and I believe it is exactly this case they try to solve.

问题是,当我将 CustomViewController 更改为从 UINavigationController 继承时,当视图控制器从 storyboard id 实例化时,视图返回空.故事板根本没有构建视图.

Problem is that when I change the CustomViewController to inherit from UINavigationController , when the view controller is instantiated from the storyboard id , the view returns empty. The storyboard isn't building the view at all.

这是可以修复的吗?

推荐答案

你没有把你的情况描述得很清楚.

You haven't described your situation very clearly.

如果你想将视图控制器推送到导航堆栈上,你必须在屏幕上有一个导航控制器.

If you want to push view controllers onto a navigation stack, you have to have a navigation controller on-screen.

您可以设置故事板,其中根视图控制器是 UINavigationController 的自定义子类.这很好用.然后,您可以使用您发布的代码实例化新的视图控制器,并将它们推送到导航控制器的导航堆栈上.将视图控制器推入导航堆栈的方法是

You can set up a storyboard where the root view controller is a custom subclass of UINavigationController. That works fine. Then you can instantiate new view controllers using the code you posted and push them onto the navigation stack of your navigation controller. The method for pushing a view controller onto the navigation stack is

pushViewController:animated:

但是,您正在使用的调用 presentViewController:animated: 是一种已弃用方法,用于呈现模态视图控制器,而不是将视图控制器推送到导航控制器堆栈上.

However, the call you are using, presentViewController:animated:, is a deprecated method to present a modal view controller, not push a view controller onto a navigation controller stack.

(该方法的新版本是呈现一个模态视图控制器是presentViewController:animated:completion:)

(The new version of that method is to present a modal view controller is presentViewController:animated:completion:)

你的第二个代码块也好不到哪里去:

Your second block of code is no better:

UIViewController *viewControllerToPresent = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: viewControllerToPresent];
[self.viewController presentViewController: nav animated:YES];

该代码创建了一个导航控制器,然后忽略它并仍然尝试将 viewControllerToPresent 呈现为 self.viewController 之上的一个模态,无论是什么.

That code creates a navigation controller, then ignores it and still attempts to present viewControllerToPresent as a modal on top of self.viewController, whatever that is.

将视图控制器推入导航控制器堆栈的方法是pushViewController:动画:.您将该消息发送给导航控制器,但导航控制器必须在屏幕上才能执行任何操作.

The method to push a view controller onto a navigation controller's stack is pushViewController:animated:. You send that message to the navigation controller, but the navigation controller has to be on-screen in order for it to do anything.

我建议您这样做:

选择故事板的初始视图控制器.从编辑器菜单中选择嵌入>导航控制器".这将创建一个新的空白导航控制器,并使其成为故事板的初始视图控制器.

Select your storyboard's initial view controller. Pick "embed in>navigation controller" from the editor menu. This will create a new, blank navigation controller and make it the initial view controller for your storyboard.

这篇关于在故事板中创建自定义 UINavigationController 不会实例化视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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