Issues with UIMoreNavigationController and UITabBarController

纵然是瞬间 提交于 2019-12-08 02:20:22

问题


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.


回答1:


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 the moreNavigationController property of the UITabBarViewController.

But the 'viewControllers' property always only holds those ViewControllers, that you have added to the TabBar.




回答2:


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 a UINavigationController managed by UITabBarController to hold any surplus child controllers. See Apple's UITabBarController reference, but you can also double-check in the UIKit framework header file UITabBarController.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.




回答3:


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.

  • Harald


来源:https://stackoverflow.com/questions/9430427/issues-with-uimorenavigationcontroller-and-uitabbarcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!