1

如何将 TabBarController 推送到视图?

该应用程序是基于视图的。在应用程序中,我有一个带有 TabBarController 的 XIB,我想将 TabBar 推到显示器上。我已经做了一切,现在我不知道我做错了什么。

这是应该推送 TabBarController 的代码:

- (void)showTabBar {
TabBar *tbc = [[TabBar alloc] initWithNibName:@"TabBar" bundle:nil];
[self presentModalViewController:tbc animated:YES];
[tbc release];
}

此时,XIB 中有一个空视图。所以当我运行这段代码时显示是白色的。还有我想在显示器上看到的 TabBarController。

谢谢你的帮助。


更新:

我应该说这是两个不同的类别。函数“showTabBar”所在的类应该推送TabBarController。第二类是TabBar。TabBar 是 TabBarController 的委托。

所以我修改了这样的答案,但我都尝试了:

[self presentModalViewController:tbc.tabBarController.view animated:YES];

错误现在看起来像这样:

Incompatible Objective-C types 'struct UIView *', expected 'struct UIViewController *' when passing argument 1 of 'presentModalViewController:animated:' from distinct Objective-C type

更新:

嘿,错误消失了。凉爽的。;)

但该应用程序正在终止。我以前见过这种行为,但我不知道该怎么做。调试器控制台这样说:

2011-01-27 15:49:32.629 Touch[3657:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <StartUp: 0x9300f20>.'
4

1 回答 1

2

因为 UITabBarController 类继承自 UIViewController 类,所以标签栏控制器有自己的视图,可以通过 view 属性访问。部署选项卡栏界面时,您必须将此视图安装为窗口的根目录。与其他视图控制器不同,标签栏界面永远不应安装为另一个视图控制器的子级。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html

What this mean is, this is not "allowed" and Apple can or may in the future reject your application. UITabBarController is intended to be added to the root view(window) in your application delegate. If you want a tabbar to be pushed on later you need to use the UIToolBar with e.g. uisegmentedcontrol or the toolbar buttons.

Even though some apps does this(e.g Wordpress app) you will later find out that you will run onto some issues in the console when presenting viewcontrollers modally.(A warning will be given).

于 2011-01-27T14:41:34.840 回答