我已经以编程方式添加了tabbarcontroller,然后向它添加了两个viewcontrollers。相同的代码是:
ExampleViewcontroller.m
self.tabController = [[UITabBarController alloc] init];
viewController1 =[[invoiceviewcontrolleralloc]
initWithNibName:@"invoiceviewcontroller" bundle:nil];
viewController1.title = @"Unpaid Invoice";
viewController2=[[remittanceviewcontrolleralloc]
initWithNibName:@"remittanceviewcontroller" bundle:nil];
tabController.view.frame = CGRectMake(0, 0, 320, 460);
self.tabController.viewControllers = [NSArray arrayWithObjects:
viewController1,viewController2,nil];
[self.view addSubview:tabController.view];
tabController.delegate=self;
self.view bringSubviewToFront:tabController.view];
和tabbarcontroller的方法如下: -
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:
(UIViewController *)viewController
{
if(tabController.selectedIndex==0)
{
[viewController2.view removeFromSuperview];
[self.view addSubview:viewController1.view];
}
else if(tabController.selectedIndex==1)
{
[viewController1.view removeFromSuperview];
[self.view addSubview:viewController2.view];
}
[self.view bringSubviewToFront:tabController.view];
}
代码运行正常但是当我点击第二个选项卡时,viewcontroller附加失去了交互性,即其中的功能不响应click。
答案 0 :(得分:0)
UITabBarController旨在位于UIVIewController层次结构的顶部,或直接以模态方式呈现。您不能将其视图放在另一个UIViewControllers视图中,并期望它能够正常工作。事实上,每当你发现自己手动将一个视图控制器的视图添加到另一个视图控制器时,你可能做错了。如果您正确使用它们,视图控制器将为您执行此操作。
请在此处阅读“创建标签栏界面”并特别注意要点:http://developer.apple.com/library/ios/ipad/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/TabBarControllers.html#//apple_ref/doc/uid/TP40011313-CH3