将新的视图控制器添加到选项卡控制器
Posted
技术标签:
【中文标题】将新的视图控制器添加到选项卡控制器【英文标题】:Add a new viewcontroller to a tab controller 【发布时间】:2013-02-15 13:07:01 【问题描述】:我有一个由 MainWindow.xib 创建的标签栏控制器。我有 4 个视图控制器。现在我想以编程方式添加第 5 项(因为直到编译时我才知道必须使用哪个类)
这是我的代码:
UIViewController * login = [[LoginUserViewController alloc] initWithNibName:@"LoginUserViewController" bundle:nil];
NSMutableArray * viewControllersArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
[viewControllersArray addObject:login];
[self.tabBarController setViewControllers:viewControllersArray
animated:YES];
但我明白了
[LoginUserViewController viewControllers]: unrecognized selector sent to instance 0x95791b0'
当我到达此代码时
UINavigationController *navController = [tabBarController.viewControllers lastObject];
LoginViewController * log = [navController.viewControllers objectAtIndex:0];
我哪里出错了?有任何想法吗?
非常感谢
【问题讨论】:
【参考方案1】:如果这是您的全部代码,那么您看起来不像是在实例化导航控制器。看:
initWithRootViewController:
在 UINavigatorClass 中。我会替换:
UINavigationController *navController = [tabBarController.viewControllers lastObject];
与:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: [tabBarController.viewControllers lastObject]];
编辑:
还有一个想法:
看起来您很幸运地使用了“tabBarController”属性,因为您可能已将其合成为 @synthesize tabBarController=tabBarController;
我强烈建议您使用最新版本的 XCode,它会自动为您执行 @synthesize。代码中最后一行应该是 self.tabBarController
【讨论】:
【参考方案2】:您忘记了最后一个选项卡是 LoginUserViewController 而不是 UINavigationController 的实例。
将 LoginUserViewController 添加到标签栏控制器后,标签栏控制器数组中的最后一个视图控制器将是 LoginUserViewController 而不是 UINavigationController 的实例
UINavigationController *navController = [tabBarController.viewControllers lastObject];
因此上面的行将在 navController 变量中返回 LoginUserViewController 的对象。
RecordsViewController *recordsViewController = [navController.viewControllers objectAtIndex:0];
因此上面的行将导致崩溃,因为 LoginUserViewController 没有 viewControllers 属性。
【讨论】:
【参考方案3】:试试这个……
- (void) setUpTabBar
FirstViewController *firstViewController = [[FirstViewController alloc]init];
firstViewController.title = @"First View";
firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
SecondViewController *secondViewController = [[SecondViewController alloc]init];
secondViewController.title = @"Second View";
secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];
ThirdViewController *thirdViewController = [[ThirdViewController alloc]init];
thirdViewController.title = @"Third View";
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];
ForthViewController *forthViewController = [[ForthViewController alloc]init];
forthViewController.title = @"Forth View";
forthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2];
UINavigationController *forthNavController = [[UINavigationController alloc]initWithRootViewController:forthViewController];
tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, forthNavController, nil];
tabBarController.delegate = self;
[self sizeViewToAvailableWindow:[tabBarController view]];
[firstNavController release];
[firstViewController release];
[secondNavController release];
[secondViewController release];
[thirdNavController release];
[thirdViewController release];
[forthNavController release];
[forthViewController release];
【讨论】:
这就是我所需要的,实例化一个 UINavigationController以上是关于将新的视图控制器添加到选项卡控制器的主要内容,如果未能解决你的问题,请参考以下文章