Monday, June 10, 2013

UITabBar selectedItem example in Objective C (iOS).

UITabBar selectedItem

The currently selected item on the tab bar.

@property(nonatomic, assign) UITabBarItem *selectedItem

Discussion of [UITabBar selectedItem]
Changes to this property show visual feedback in the user interface. The selected and unselected images displayed by an item are automatically created based on the alpha values in its original image property that you set. The default value is nil.

UITabBar selectedItem example.
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];

Example of [UITabBar selectedItem].
Can't you just call your method to select a tab whenever you display the view? Like so:

[self activateTab:1];
To change which tab bar item is selected use:

[myTabBar setSelectedItem:myTabBarItem];
Where myTabBarItem is your UITabBarItem instance for the relevant view.

UITabBar selectedItem example.
- (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;

End of UITabBar selectedItem example article.