UIMoreNavigationController 和 UITabBarController 的问题
这个问题已经困扰我一段时间了,但我想我终于弄清楚出了什么问题;我认为我现在只需要一个解决方案...
这是该应用程序的背景。用户可以使用大约 6 个不同的选项卡,并使用 UITabBarController 显示。每个选项卡都是 UINavigationController 内的自定义 UIViewController 子类。所有 6 个选项卡均设置在 nib 文件 (MainWindows.xib) 内。
我需要能够隐藏和显示不同的选项卡,具体取决于用户是否登录以及他们登录的用户。我的工作方式如下:
在应用程序启动时(应用程序:didFinishLaunching:...),六个选项卡存储到我拥有的 NSMutableArray 中。这工作正常......
当用户登录或注销时,我访问他可以从 NSMutableArray 使用的选项卡并将它们添加到 UITabBarController 中,如下所示:
[tabBar setViewControllers: [NSArray arrayWithObjects:
[viewControllers objectAtIndex:1],
[viewControllers objectAtIndex:5],
nil] animated:YES];
viewControllers 是我之前使用 6 个选项卡制作的 NSMutableArray。在创建它之后对其进行 NSLog 给出了这个,这正是我所期望的:
2012-02-24 11:45:57.690 [redacted][26155:207] (
"<UINavigationController: 0x8249db0>",
"<UINavigationController: 0x841a3f0>",
"<UINavigationController: 0x824be40>",
"<UINavigationController: 0x824dbd0>",
"<UINavigationController: 0x824e810>",
"<UINavigationController: 0x841dfb0>"
)
但是,当我从最后一个自定义视图控制器(位于该列表中最后一个导航控制器内)打印 self.parentViewController 的值时,我得到了这个:
2012-02-24 11:54:51.247 [REDACTED][26306:207] <UIMoreNavigationController: 0x826ab00>
2012-02-24 11:54:51.248 [REDACTED][26306:207] <UITabBarController: 0x8257c50>
第一行是 self.parentViewController,第二行是 self.parentViewController.parentViewController
这似乎表明继承关系是:
UITabBarController -> UIMoreNavigationController -> MyCustomController
但是,当我打印 [self.parentViewController.parentViewController viewControllers] 时,
我仍然得到:
(
"<UINavigationController: 0x8259770>",
"<UINavigationController: 0x825aa60>",
"<UINavigationController: 0x825bec0>",
"<UINavigationController: 0x82612c0>",
"<UINavigationController: 0x8261ec0>",
"<UINavigationController: 0x8263b00>"
)
UIMoreNavigationController 去哪儿了?谁能解释一下发生了什么事吗?我遇到与此相关的问题,因为我使用该数组,但是最后一个 UINavigationController 不是它声称的对象。
我有一种预感,苹果正在幕后摆弄对象,以便让程序员更容易......
我会尝试回答您对代码如何结构,如何使用不同对象的任何问题,或测试一些代码。预先非常感谢您。
This problem has been plaguing me for a little while, but I've think I've finally figured out what's wrong; I think I just need a solution now...
Here's the background on the app. There are about 6 different tabs which the user can use, displayed using a UITabBarController. Each of these tabs is a custom UIViewController subclass, inside a UINavigationController. All 6 tabs are set up inside a nib file (MainWindows.xib).
I need to be able to hide and show different tabs depending on if the user is logged on or not, and who they're logged into. I have this working like so:
On app launch (application: didFinishLaunching: ...), the six tabs are stored into a NSMutableArray which I have. This works fine...
When a user logs in or out, I access the tabs that (s)he can use from the NSMutableArray and add them to the UITabBarController like so:
[tabBar setViewControllers: [NSArray arrayWithObjects:
[viewControllers objectAtIndex:1],
[viewControllers objectAtIndex:5],
nil] animated:YES];
viewControllers is the NSMutableArray which I made earlier with the 6 tabs. Doing NSLog on it just after I create it gives this, which is what I expect:
2012-02-24 11:45:57.690 [redacted][26155:207] (
"<UINavigationController: 0x8249db0>",
"<UINavigationController: 0x841a3f0>",
"<UINavigationController: 0x824be40>",
"<UINavigationController: 0x824dbd0>",
"<UINavigationController: 0x824e810>",
"<UINavigationController: 0x841dfb0>"
)
However, when I print the value of self.parentViewController from the last custom view controller, which is inside the last navigation controller in that list, I get this:
2012-02-24 11:54:51.247 [REDACTED][26306:207] <UIMoreNavigationController: 0x826ab00>
2012-02-24 11:54:51.248 [REDACTED][26306:207] <UITabBarController: 0x8257c50>
The first line is self.parentViewController, the second is self.parentViewController.parentViewController
This seems to indicate the heirachy is:
UITabBarController -> UIMoreNavigationController -> MyCustomController
However when I print [self.parentViewController.parentViewController viewControllers]
I still get:
(
"<UINavigationController: 0x8259770>",
"<UINavigationController: 0x825aa60>",
"<UINavigationController: 0x825bec0>",
"<UINavigationController: 0x82612c0>",
"<UINavigationController: 0x8261ec0>",
"<UINavigationController: 0x8263b00>"
)
Where's the UIMoreNavigationController gone? Can anyone explain what's going on? I'm encountering problems related to this because I use that array, however the last UINavigationController is not the object it claims to be.
I have a hunch that apple is fiddling with the objects behind the scenes in order to make it easier for the programmer...
I'll try and reply to any questions you have with how the code is structured, how I use different objects, or to test some code. Thank you very much in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上Tabbar的moreViewController是一个
UIMoreNavigationController
。 (您可以查看 GitHub 上的 私有标头< /a>)正如文档所述,viewController 属性仅包含已添加到选项卡栏的 viewController:
您也不得在 viewControllers 属性中存储的视图控制器数组中查找更多导航控制器。选项卡栏控制器不包括该对象数组中的更多导航控制器。
请参阅此处的文档:UITabbarViewController。
无论如何,我不明白,你的问题到底是什么。如果您需要访问
UIMoreNavigationController
,请通过UITabBarViewController
的moreNavigationController
属性进行访问。但“viewControllers”属性始终只保存您已添加到 TabBar 的那些 ViewController。
Actually the moreViewController of the Tabbar IS a
UIMoreNavigationController
. (You can have a look at the private header on GitHub)As the documentation states, the viewController property only contains the viewControllers, which you have added to the tabbar:
You must also not look for the More navigation controller in the array of view controllers stored in the viewControllers property. The tab bar controller does not include the More navigation controller in that array of objects.
See documentation here: UITabbarViewController.
Anyway i don't understand, what your problem exactly is. If you need to access the
UIMoreNavigationController
do it via themoreNavigationController
property of theUITabBarViewController
.But the 'viewControllers' property always only holds those ViewControllers, that you have added to the TabBar.
既然你没有说你的实际问题是什么,我也无话可说。但我很确定不存在 UIMoreNavigationController 这样的类。 “更多”导航控制器只是一个由
UITabBarController
管理的 UINavigationController,用于保存任何多余的子控制器。请参阅 Apple 的 UITabBarController 参考,但是您还可以仔细检查 UIKit 框架头文件UITabBarController.h
。相应地,我无法重现显示
的调试输出。在我的环境(SDK 5.0)中,无论我是否检查自定义视图控制器的父级或选项卡栏控制器的子级,我都只会得到
。Since you don't say what your actual problem is, I can't say anything about that. But I'm quite sure that there is no such class as
UIMoreNavigationController
. The "More" navigation controller is just aUINavigationController
managed byUITabBarController
to hold any surplus child controllers. See Apple's UITabBarController reference, but you can also double-check in the UIKit framework header fileUITabBarController.h
.Correspondingly, I was unable to reproduce your debug output where you show
<UIMoreNavigationController: 0x826ab00>
. In my environment (SDK 5.0), I just get<UINavigationController: 0x12345678>
, regardless of whether I check my custom view controller's parent, or the tab bar controller's children.只是为了确认是否有同样的问题。我相信在 UITabBarController 创建了 UIMoreNavigationController 的情况下使用 setViewController: 会被破坏。问题是,当更改选项卡栏的视图控制器时,更多的导航控制器未正确维护 - 控制器层次结构正在损坏。我已向 Apple 提交了错误,但尚未收到回复。
Just to confirm a have the same problem. I believe that using setViewController: is broken in situations a UIMoreNavigationController has been created by the UITabBarController. The issue is that when changing the tab bar's view controllers, the more navigation controller is not correctly maintained - the controller hierarchy is getting corrupted. I have filed a bug with Apple but have not yet received a reply.