设置tabBarController viewControllers属性,其数组文字超过5个viewControllers

时间:2014-10-31 00:02:20

标签: ios objective-c uitabbarcontroller

所以我试图将viewController添加到tabBarController,这应该强制moreViewController,因为我已经有5个,我将添加第6个。我使用数组文字设置viewControllers的{​​{1}}属性。这似乎非常简单,但我似乎无法在堆栈或Apple的文档中找到任何可能无法像这样初始化它的文档。

在我尝试调试问题时,下面是代码和NSLog的输出。我无法弄清楚为什么我可以将tabBarController属性初始化为6个对象而不是5个对象,如下所示:

viewControllers

以下是我为tabBar设计样式的另一种方法的摘录:

@property (nonatomic, retain) UINavigationController* navVC1;
@property (nonatomic, retain) UINavigationController* navVC2;
@property (nonatomic, retain) UINavigationController* navVC3;
@property (nonatomic, retain) UINavigationController* navVC4;
@property (nonatomic, retain) UINavigationController* navVC5;
@property (nonatomic, retain) UINavigationController* navVC6;

@property (nonatomic, retain) customVC1* vc1;
@property (nonatomic, retain) customVC2* vc2;
@property (nonatomic, retain) customVC3* vc3;
@property (nonatomic, retain) customVC4* vc4;
@property (nonatomic, retain) customVC5* vc5;
@property (nonatomic, retain) customVC6* vc6;

self.vc1 = [customVC1 loadFromNib];
self.vc2 = [customVC2 loadFromNib];
self.vc3 = [customVC3 loadFromNib];
self.vc4 = [customVC4 loadFromNib];
self.vc5 = [customVC5 loadFromNib];
self.vc6 = [customVC6 loadFromNib];

self.navVC1 = [[[CustomNavController alloc] initWithRootViewController:self.vc1] autorelease];
self.navVC2 = [[[CustomNavController alloc] initWithRootViewController:self.vc2] autorelease];
self.navVC3 = [[[CustomNavController alloc] initWithRootViewController:self.vc3] autorelease];
self.navVC4 = [[[CustomNavController alloc] initWithRootViewController:self.vc4] autorelease];
self.navVC5 = [[[CustomNavController alloc] initWithRootViewController:self.vc5] autorelease];
self.navVC6 = [[[CustomNavController alloc] initWithRootViewController:self.vc6] autorelease];

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = @[self.navVC1, self.navVC2, self.navVC3, self.navVC4, self.navVC5, self.navVC6];

NSArray* tBArray = @[self.navVC1, self.navVC2, self.navVC3, self.navVC4, self.navVC5, self.navVC6];

NSLog(@"Here's the number of VCs        %d",[self.tabBarController.tabBar.items count]); // this outputs 5
NSLog(@"Here's the array count        %d",[tBArray count]); // this outputs 6

所以应用程序崩溃了我,这里出了界限错误:

int index = 0; UITabBarItem* tb = nil; tb = [self.tabBarController.tabBar.items objectAtIndex:index++]; [tb setTitle:@"vc1"]; tb = [self.tabBarController.tabBar.items objectAtIndex:index++]; [tb setTitle:@"vc2"]; tb = [self.tabBarController.tabBar.items objectAtIndex:index++]; [tb setTitle:@"vc3"]; tb = [self.tabBarController.tabBar.items objectAtIndex:index++]; [tb setTitle:@"vc4"]; tb = [self.tabBarController.tabBar.items objectAtIndex:index++]; [tb setTitle:@"vc5"]; tb = [self.tabBarController.tabBar.items objectAtIndex:index++]; // this is the out of bounds crash [tb setTitle:@"vc6"];

编辑:以下是解决方案:

感谢大家的帮助。弄清楚,一旦有超过5个标签,就无法使用*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]'将属性直接应用于tabBarItems,因为一旦你超过5,它似乎是第5个标签变为objectAtIndex的数组这是viewControllers。我刚刚重写了代码,将所有属性直接应用到moreViewController,然后再将它们添加到viewControllers,如下所示:

tabBar

3 个答案:

答案 0 :(得分:2)

即使viewControllers中有6个视图控制器,tabBar仍然只有5个items

来自UITabBarController文档:

  

您永远不应直接访问标签栏控制器的标签栏视图。要配置选项卡栏控制器的选项卡,请将为每个选项卡提供根视图的视图控制器分配给viewControllers属性。

设置视图控制器的title属性;不要直接访问tabBar

答案 1 :(得分:1)

UITabBarController最多只能包含5个标签。如果您为viewControllers分配了5个以上的视图控制器,则选项卡栏将仅显示前4个选项卡和第5个选项卡,名为更多,其中显示了每个剩余选项卡的行视图的表格视图。 这里的要点是,在这种情况下,UITabBarController表示它拥有完整数量的视图控制器,而UITabBar最多显示5个项目。

[tabBarController.tabBar.items objectAtIndex:5]是导致您的应用崩溃的原因。

答案 2 :(得分:-2)

不确定为什么不起作用,但您也可以使用

[self.tabBarController addChildViewController:viewController];

每一个