与两个 UINavigationController 共享与 rootViewController 相同的 UIViewController [英] Sharing the Same UIViewController as the rootViewController with Two UINavigationControllers

查看:20
本文介绍了与两个 UINavigationController 共享与 rootViewController 相同的 UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我决定用不同的方式解决我的问题.我没有尝试直接重用相同的 UIViewController,而是使用了两个设置为 rootViewControllers 的普通 UIViewControllers.在他们的 loadView 方法中,他们调用 [storyboard instantiateViewControllerWithIdentifier:] 来获取前一个 UIViewController 并将他们的视图设置为控制器的视图.无论如何,这可能是首选方法,因为我需要设置几个变量和委托.

Update: I have decided to go a different route with my problem. Instead of trying to reuse the same UIViewController directly, I use two vanilla UIViewControllers that are set as rootViewControllers. In their loadView methods, they make a call to [storyboard instantiateViewControllerWithIdentifier:] to get the former UIViewController and set their views to the controller's view. This is probably the preferred approach anyway, since I need to set several variables and delegates.

我有一个 UIStoryBoard,其中有一个 UITabBarController 作为与两个 UINavigationControllers 连接的入口点.它们中的每一个都共享一个共同的 UIViewController 作为它们的根视图控制器.当应用程序启动时,第一个 UITabBarItem 被选中,视图按预期加载.但是,当我选择第二个 UITabBarItem 时,相同的视图不可见.我看到了黑色背景的 UINavigationBar.我是否在 Storyboard 界面上做错了什么,或者我是否需要通过每个 UINavigationController 的 方法手动实例化 UIViewController——例如 loadView?

I have a UIStoryBoard with a UITabBarController as the entry point connected with two UINavigationControllers. Each of those share a common UIViewController as their root view controller. When the app starts, the first UITabBarItem is selected and the view loads as-expected. However, when I select the second UITabBarItem, the same view is not visible. I see the UINavigationBar with a black background. Am I doing something incorrect with the Storyboard interface, or do I need to manually instantiate the UIViewController via each UINavigationController's method--loadView for instance?

推荐答案

奇怪的是,这是一个没人问的问题.据我所知,无法共享 rootViewController,我认为这无疑是一个错误,因为当您检查情节提要上的连接时,您可以看到视图控制器连接到两个导航控制器.我认为这是故事板中的一个缺陷,因为复制 viewController 并重新应用它们的所有连接很容易出错,并使故事板过于复杂.

Strangely this is a question that no one else is asking. As far as I know it is not possible to share the rootViewController which I think is without a doubt a bug since when you inspect the connection on the storyboard you can see that the view controller is connected to both navigation controllers. I consider this a flaw in storyboarding because duplicating viewControllers and reapplying all of their connections is quite error prone and makes the storyboards overly complex.

我看到了您对问题的解决方案.像这样的解决方法让我怀疑 iOS 中当前的故事板功能是否已准备好创建应用程序.我认为情节提要存在概念上的问题,Apple 需要确定情节提要上的 viewController 是代表一个实例还是只代表一个类,现在它并不一致,因为您可以看到多个 segues 实际上可以指向相同的 viewController 但实际上每个 segue 都有自己的实例,为什么 rootViewController 连接也不遵循?,我不知道.

I see your solution to the problem. Workarounds like this make me question if the current storyboard functionality in iOS is ready for creating apps. I think that there is a conceptual problem with the storyboards, Apple needs to decide if a viewController on a storyboard represents an instance or if it represents just the class, right now it is not consistent as you can see that multiple segues can actually point to the same viewController but in reality each segue has its own instance, why this is not also followed for rootViewController connections?, I don't know.

请注意,在您的解决方案中,请考虑 Apple 文档中的以下内容:

Just as a note, with your solution take into account the following from Apple's documentation:

重要视图控制器是其视图及其创建的任何子视图的唯一所有者.它负责创建这些视图并在适当的时间放弃它们的所有权,包括在内存不足的情况下和视图控制器时本身已发布.如果您使用情节提要或 nib 文件来存储视图对象,则每个视图控制器对象会在视图控制器请求时自动获取这些视图的自己的副本.但是,如果您手动创建视图,则应该永远不要对多个视图控制器使用相同的视图对象."

"Important A view controller is the sole owner of its view and any subviews it creates. It is responsible for creating those views and for relinquishing ownership of them at the appropriate times, including during low-memory conditions and when the view controller itself is released. If you use a storyboard or a nib file to store your view objects, each view controller object automatically gets its own copy of these views when the view controller asks for them. However, if you create your views manually, you should never use the same view objects with multiple view controllers."

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html

这篇关于与两个 UINavigationController 共享与 rootViewController 相同的 UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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