将新的viewcontroller添加到选项卡控制器

时间:2013-02-15 13:07:01

标签: ios cocoa-touch uiviewcontroller uinavigationcontroller uitabbarcontroller

我有一个由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];

我哪里错了?有任何想法吗?

非常感谢

3 个答案:

答案 0 :(得分:1)

您忘记了最后一个标签是LoginUserViewController,而不是UINavigationController的实例。

将LoginUserViewController添加到标签栏控制器后,标签栏控制器数组中的最后一个视图控制器将是LoginUserViewController,而不是UINavigationController的实例

UINavigationController *navController = [tabBarController.viewControllers lastObject];

因此上面的行将在navController变量中返回LoginUserViewController的对象。

RecordsViewController *recordsViewController = [navController.viewControllers objectAtIndex:0];

因此,由于LoginUserViewController没有viewControllers属性,因此上面的行会导致崩溃。

答案 1 :(得分: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 :(得分:0)

试试这个......

  - (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];
}
相关问题