我正在构建一个应用程序,其中 Root View /窗口是基于选项卡的 View (使用用于创建基于选项卡的 iPhone 应用程序的 XCode 向导创建),但应用程序中还有一个我想要的点创建另一个基于选项卡的 View 并以模态方式呈现它。
我在 IB 中创建基于模式选项卡的 View 时遇到了很多麻烦,最终我只是在代码中完成了它,有点像这样:
// *** In the event handler that causes the second-tab view to be presented ***
MyTabViewController *tabVC = [[MyTabViewController alloc] init];
[self presentModalViewController:tabVC.tabBarController animated:YES];
[tabVC release];
// *** Inside init() definition in MyTabViewController.m ***
UIViewController *vc1 = [[MyViewController1 alloc] init];
UIViewController *vc2 = [[MyViewController2 alloc] init];
tabBarController_ = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController_.viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController_.selectedIndex = 0;
这工作得很好,直到我开始尝试写入 tabBarController_.tabBar.items 来设置按钮的标题和图像,它显然不想让您为 TabBarController 拥有的 TabBar 执行此操作,给出这个错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'Directly modifying a tab bar managed by a tab bar controller is not allowed.'
因此,我尝试返回使用 Interface Builder 实现 MyTabViewController,以便我可以在那里设置按钮,但我无法弄清楚。以下是我为达到现在的目标而采取的步骤:
- 在 XCode 中创建了源自 UIViewController 的新类,并选中了“with XIB”选项。
- 将 TabBarController 拖入 XIB(后面有黄色球的那个)。
我不明白的是如何让 TabBar 接管 View 。现在,XIB 附带的 View 自动为空,并且我的 UITabBarController 与它完全断开连接。如果我尝试将 TabBar 从 UITabBarController 中拖动到 View 中,它似乎会创建一个新的 TabBar,而不是将我的 TabBar 放置到 View 中,以便它占据整个 View 。
如果我没有清楚地解释这一点,我很抱歉,但我真的很难理解 TabBarController 和 View 之间的联系,即无法弄清楚如何让 TabBarController 实际显示。
对此的任何帮助将不胜感激。如果有帮助的话,我附上了 IB 的屏幕截图。
最佳答案
标签栏 Controller 从它管理的 View Controller 中获取标签的标签和图像。标签来自 View Controller 的 title
。该图像来自 View Controller 的 tabBarItem
属性。您可以在 MyViewController1 和 MyViewController2 的 init 方法中设置这两者。
- (id)init {
if (self = [super initWithNibName:@"MyViewController" bundle:nil]) {
self.title = @"My View Controller";
UIImage* anImage = [UIImage imageNamed:@"MyViewControllerImage.png"];
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:anImage tag:0];
self.tabBarItem = theItem;
[theItem release];
}
return self;
}
每个 View Controller 都有一个 view
属性。标签栏 Controller 也是一个 View Controller ,因此它也有一个 view
属性。将标签栏 Controller 的 view
属性添加到当前空白的 View 中。
示例:
[view addSubview:tabBarController.view];
参见documentation如果您有疑问。非常完整。
关于iphone - Interface Builder 中的 UITabBarControllers 遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2536187/