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

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

问题描述

更新:我已决定针对我的问题采取不同的路线。
我没有尝试直接重用相同的 UIViewController ,而是使用两个设置为 rootViewControllers 的vanilla 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.

我有一个带有 UITabBarController的 UIStoryBoard 作为与两个 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我认为毫无疑问是一个bug,因为当你检查故事板上的连接时,你可以看到视图控制器连接到两个导航控制器。我认为这是故事板中的一个缺陷,因为复制viewControllers并重新应用它们的所有连接非常容易出错并且使故事板过于复杂。

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都有自己的实例,为什么rootseController连接也不遵循?,我不知道。

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:

重要信息视图控制器是其视图及其创建的任何子视图的唯一所有者。它负责创建这些视图以及在适当的时候放弃它们的所有权,包括在低内存条件下和视图控制器本身被释放时。如果使用storyboard或nib文件来存储视图对象,每个视图控制器对象自动获取它视图c时拥有这些视图的副本ontroller要求他们。但是,如果手动创建视图,则不应将相同的视图对象与多个视图控制器一起使用。

"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

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

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