0

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

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

非常感谢

4

3 回答 3

1

您忘记了最后一个选项卡是 LoginUserViewController 而不是 UINavigationController 的实例。

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

UINavigationController *navController = [tabBarController.viewControllers lastObject];

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

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

因此,上面的行将导致崩溃,因为 LoginUserViewController 没有 viewControllers 属性。

于 2013-02-15T13:21:51.847 回答
1

如果这是您的全部代码,那么您看起来不像是在实例化导航控制器。看着:

initWithRootViewController:

在 UINavigator 类中。我会替换:

UINavigationController *navController = [tabBarController.viewControllers lastObject];

和:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: [tabBarController.viewControllers lastObject]];

编辑:还有一个想法:看起来您很幸运地使用了“tabBarController”属性,因为您可能已经合成了它,因为@synthesize tabBarController=tabBarController;
我强烈建议您使用最新版本的 XCode,它会自动为您执行 @synthesize。代码中最后一行应该是 self.tabBarController

于 2013-02-15T13:22:13.073 回答
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];
}
于 2013-02-15T13:21:40.833 回答