2

In iOS, the TabBar property in the TabBarController is read only. How can I associate a custom item with a particular view controller? How do I access the UITabBarItems inside the tabBar?

Like this

CustomView *custom = [[CustomView alloc] init];
UITabBarItem *customTab = [[UITabBarItem alloc] initWithTitle:@"Custom" image:[UIImage imageNamed:@"custom.png"] tag:0];
SecondView *second = [[SecondView alloc] init];
UITabBarItem *secondTab = [[UITabBarItem alloc] initWithTitle:@"Next" image:[UIImage imageNamed:@"next.png"] tag:1];
NSArray *views = [NSArray arrayWithObjects:custom,second,nil];
[tabBarController setViewControllers:views];
//how do I set the individual TabBarItems (customTab,secondTab) to be associated
//with the views in question?  tabBarController.tabBar is read only
Cœur
  • 37,241
  • 25
  • 195
  • 267
Daddy
  • 9,045
  • 7
  • 69
  • 98
  • Trying to modify the tabBar with tabBar:setItems:animated is a no-no. *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Directly modifying a tab bar managed by a tab bar controller is not allowed.' – Daddy Oct 11 '10 at 16:11

1 Answers1

5

Inside each view controller, you can set a tabBarItem property. If the view controller is owned by a UITabBarViewController the associated item on the tab bar will be updated accordingly.

Something like this

-(void)viewDidLoad {
    [super viewDidLoad];
    UITabBarItem *tbi = [[UITabBarItem alloc] initWithTitle:yourTitle image:yourIcon tag:yourTag];
    [self setTabBarItem:tbi]
    [tbi release];
}

You are not restricted to perform this operation in the viewDidLoad method, obviously.

sigsegv
  • 447
  • 8
  • 17