UITabBar 和超过 1 个 UINavigationController
我目前正在开发一个应用程序,需要我有不同的 UINavigationControllers - 所以我正在使用选项卡栏并尝试使用 UITabBar 在它们之间进行交换,所以我在应用程序委托中有一点像这样的代码:
// Setting up the views for the tab controller
Friends *friends = [[[Friends alloc] initWithNibName:@"Friends" bundle:[NSBundle mainBundle]] autorelease];
WifiManager *wifi = [[[WifiManager alloc] initWithNibName:@"WifiManager" bundle:[NSBundle mainBundle]] autorelease];
UINavigationController *locationController = [[UINavigationController alloc] initWithRootViewController:wifi];
UINavigationController *friendsController = [[UINavigationController alloc] initWithRootViewController:friends];
//Set up the tab controller
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers =
[NSArray arrayWithObjects:locationController, friendsController, nil];
//Add the tab bar to the window
[window addSubview:tabBarController.view];
这将编译并加载第一个 UINavigationController 但当我单击另一个导航控制器时,我得到:
*** -[NSCFData tabBarItem]: unrecognized selector sent to instance 0x1152b0'
最奇怪的部分是我可以将选项卡控制器与单个 UINavigationController 一起使用,并且一切正常,但是当我尝试添加第二个它,它惨遭失败 - 有人对我在这里做错了什么有任何想法吗?
提前谢谢你
詹姆斯
I'm currently working on an app that require that I have different UINavigationControllers - So I'm using a tab bar and attempting to use a UITabBar to swap between them so I have in the app delegate a bit of code like so:
// Setting up the views for the tab controller
Friends *friends = [[[Friends alloc] initWithNibName:@"Friends" bundle:[NSBundle mainBundle]] autorelease];
WifiManager *wifi = [[[WifiManager alloc] initWithNibName:@"WifiManager" bundle:[NSBundle mainBundle]] autorelease];
UINavigationController *locationController = [[UINavigationController alloc] initWithRootViewController:wifi];
UINavigationController *friendsController = [[UINavigationController alloc] initWithRootViewController:friends];
//Set up the tab controller
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers =
[NSArray arrayWithObjects:locationController, friendsController, nil];
//Add the tab bar to the window
[window addSubview:tabBarController.view];
This will compile and will load up the first UINavigationController but when I click on the other navigation controller I get:
*** -[NSCFData tabBarItem]: unrecognized selector sent to instance 0x1152b0'
The strangest part is I can use the tab controller with a single UINavigationController and everything works as it should but when I try and add the second one it fails miserably - has anyone got any ideas as to what I'm doing so wrong here?
Thank you in advance
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否验证过每个单一视图控制器(Friends 和 WifiManager)在只有一个视图控制器时都可以工作? 您的问题可能不是“两个控制器”,而是“一个控制器坏了”。
Have you verified that each of the single view controllers (Friends and WifiManager) work when there is only one? It could be that your problem is not "two controllers", but "one controller that is broken."
该代码应该可以工作。
我唯一能想到的建议是不要自动释放您正在创建的视图控制器。
That code should work.
The only thing I can think to suggest, is to not autorelease the view controllers you are creating.
我有一个以类似方式工作的应用程序。 我处理这个问题的方法是将其抽象得更多一些。 也就是说,让顶层(默认窗口和其他内容下方)仅是选项卡栏控制器。 我建议在这里派生一个自定义类,以便您可以获取代码。 然后让每个导航栏控制器驻留在该选项卡栏内(在笔尖结构内),但只需在显示它们时担心添加它们。
使用 Interface Builder:使用 IB 非常简单。 在我的 MainWindow.xib 文件中,顶层有所有正常的东西,窗口,通用 UITabBarController 和我希望推送到每个 UINavigationController 上的 UIViewController,我们会说 UIViewController 1b 和 2b(1a 和 2a 是两个 UIViewController)是每个导航栏的默认视图)。 我有 UITabBar 和两个 UINavigationController 嵌套在 UITabBarController 中。 在每个中,我按顺序都有一个 UINavigationBar、一个 UIViewController 和一个 UITabBarItem。 这是我的代码在应用程序委托中的样子:
然后,当我想使用导航栏时,我会这样做:
这就是我让它工作所需的一切(我可能忘记了一些 IB 接线)。
我最后的想法是,该捆绑包可能会弄乱您的导航栏。 我从未使用过它,也不知道它的优点和缺点,但你可以尝试杀死它,看看它是否是一个解决方案,至少是暂时的。
I have an application that works in a similar way. The way I handle this is to abstract it a little more. That is to say, let the top level (below the default window and stuff) be only the tab-bar controller. I would suggest deriving a custom class here so that you can get at the code. Then have each nav-bar controller reside within that tab-bar (within the nib-structure), but only worry about adding them when they are displayed.
Using Interface Builder: It was pretty simple to do with IB. In my MainWindow.xib file, the top level has all the normal stuff, Window, the generic UITabBarController and the UIViewControllers that I wish to push onto each UINavigationController, we'll say UIViewController 1b and 2b (1a and 2a are the two UIViewControllers that are the default views for each respective navbar). Nested within the UITabBarController, I have the UITabBar and my two UINavigationControllers. Within each, I have a UINavigationBar, a UIViewController, and a UITabBarItem, in that order. Here's what my code looks like in the app delegate:
Then when I want to make use of the navbars I do this:
That's all it takes for me to get it to work (I might have forgotten some IB wiring).
My final thought is that the bundle might be messing with your navbars. I have never used it and don't know much about the pros and cons, but you might try killing that to see if it's a fix, at least temporarily.