0

I try to create tab bar controller as programatically. It is ok but I can not set title to tab bar items.. How can I do this?

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

myTabBarController = [[UITabBarController alloc] init];        
tab1 = [[ZiyaretFormTab1 alloc] initWithNibName:@"ZiyaretFormTab1" bundle:nil];   
tab2 = [[ZiyaretFormTab2 alloc] initWithNibName:@"ZiyaretFormTab2" bundle:nil];   
tab3 = [[ZiyaretFormTab3 alloc] initWithNibName:@"ZiyaretFormTab3" bundle:nil];  
tab4 = [[ZiyaretFormTab4 alloc] initWithNibName:@"ZiyaretFormTab4" bundle:nil];    
tab5 = [[ZiyaretFormTab5 alloc] initWithNibName:@"ZiyaretFormTab5" bundle:nil];  

UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"title" image:nil tag:0];
tab1.tabBarItem = item;

myTabBarController.viewControllers = [NSArray arrayWithObjects: tab1, tab2,tab3,tab4,tab5,nil]; 


[self.view addSubview:myTabBarController.view];    
myTabBarController.selectedIndex=0;

}
Hacer Akac
  • 175
  • 1
  • 7
  • 18

1 Answers1

0
UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];

1 in this line means, that you try to get second object from array.

Also UITabBarItem has nice method initWithTitle:image:tag:. And this is link to documenttion its very helpfull.

My solve:

1) Create some items with:

initWithTitle:image:tag:

2) Add them to your tabbar with tab bar method:

- (void)setItems:(NSArray *)items animated:(BOOL)animated
kaspartus
  • 1,365
  • 15
  • 33
  • I didnt understand actually what should I do objectAtIndex:0 ? – Hacer Akac Jan 28 '12 at 10:32
  • I know nothing about myTabBarController. How many items does it have? Why do you get second item? Is there second item on your tabBar. – kaspartus Jan 28 '12 at 10:55
  • there is 5 item in my tab bar. I also try set objectAtIndex:0 but it not works still. – Hacer Akac Jan 28 '12 at 11:01
  • Hm, I post link to Apple documentation to UITabBarItem. There is no `setTitle` methods in it. – kaspartus Jan 28 '12 at 11:06
  • I edited my question such as your way but it still not works :S how can I do this? – Hacer Akac Jan 28 '12 at 11:25
  • It's not my way. Try [myTabBarController.tabBar setItems:[NSArray arrayWithObjects:item1,item2,nil] animated: FALSE]; – kaspartus Jan 28 '12 at 11:32
  • But your way is normal too. Try to use `- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated` instead of `myTabBarController.viewControllers = ...` – kaspartus Jan 28 '12 at 11:37