Search code examples
xcode4uitabbarcontrollerios5

setTitle of UITabBarController crashed in iOS 5 for more than 5 items?


Before updating my Xcode from 4.1 to 4.2 the following was code was working fine with iOS 4.3, but after upgrading Xcode and test my app on iPhone with iOS 5 it keep crashing on startup!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:tabController.view];
[[tabController.tabBar.items objectAtIndex:0] setTitle:@"Tab1"];
[[tabController.tabBar.items objectAtIndex:1] setTitle:@"Tab2"];
[[tabController.tabBar.items objectAtIndex:2] setTitle:@"Tab3"];
[[tabController.tabBar.items objectAtIndex:3] setTitle:@"Tab4"];
[[tabController.tabBar.items objectAtIndex:4] setTitle:@"Tab5"];
[[tabController.tabBar.items objectAtIndex:5] setTitle:@"Tab6"];
[[tabController.tabBar.items objectAtIndex:6] setTitle:@"Tab7"];
[[tabController.tabBar.items objectAtIndex:7] setTitle:@"Tab8"];
[[tabController.tabBar.items objectAtIndex:8] setTitle:@"Tab9"];
[self.window makeKeyAndVisible];
return YES;
}

It throws the following error:

2011-10-15 15:13:31.238 Mobile[2347:707] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
*** First throw call stack:
(0x311138bf 0x382b81e5 0x3106b275 0x336b 0x330e57eb 0x330df3bd 0x330ad921 0x330ad3bf 0x330acd2d 0x33c98e13 0x310e7553 0x310e74f5 0x310e6343 0x310694dd 0x310693a5 0x330de457 0x330db743 0x2acd 0x2a28)

If I kept only the first 5 setTitle it will work, but if its more than 5 it will crash!

Please note that I'm creating the UITabBarController in IB not programatically.


Solution

  • Try accessing the viewControllers array on the tabController instead and editing the tabItem on the View Controller directly.

    [[tabController.viewControllers objectAtIndex:0].tabBarItem setTitle:@"Tab1"];