1
votes

How do I set the default selected UITabBarItem in an UITabBar that is within an UIView, not an UITabBarController?

Just to clarify, the UIView does implement the protocol and the didSelectItem method works. At run-time, the tabbar works and the tabbaritems selected when the user touches them. My problem is setting the default selected item.

If i use [myTabbar setSelectedItem] within the didSelectItem method it works. But outside of it, it doesn't (for example, in the viewDidLoad method of my UIView).

Thanks!

2

2 Answers

2
votes

You might want to post some sample code. I just ran a quick test in -viewDidLoad:

UITabBarItem *about = [[UITabBarItem alloc] initWithTitle:@"About" image:[UIImage imageNamed:@"About.png"] tag:0];
NSArray *tabBarItems = [[NSArray alloc] initWithObjects:about,nil];
[tabBar setItems:tabBarItems animated:NO];
[tabBar setSelectedItem:about];
[tabBarItems release];
[about release];

which worked fine, or at least worked as I expected.

4
votes

Thank you for your approatch, but I still have a problem that the didSelectedItem is not called when I select the item with setSelectedItem. Any Idea?

actually, I did use it a little bit different:

[tabbar setSelectedItem:[tabbar.items objectAtIndex:0]];

finally I solved it this way....

- (void)viewDidLoad 
{
 UITabBarItem *item = [myTabBar.items objectAtIndex:0];
 [self.myTabBar setSelectedItem:item];
 if(tab1Exam == nil){
  self.tab1Exam = [[CurExam alloc] initWithNibName:@"CurExam" bundle:nil];
 [self.view insertSubview:tab1Exam.view belowSubview:myTabBar];
 if (currentViewController != nil)
  [currentViewController.view removeFromSuperview];
 currentViewController = tab1Exam;
 }
}