Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the Tab bar item 1 to be selected by default in iphone?

Tags:

I am new to iPhone development. I am creating a view based application. I have added a tab bar in my view (and not a tab bar controller). By setting the tag vale of the tab bar item to 1, 2, I have loaded the view for each tab bar on tabbar item click event.

I want the tab bar 1 to be selected by default. What should I do for that?

Here is my code:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {     NSLog(@"didSelectItem: %d", item.tag);     [self activateTab:item.tag]; }  - (void)activateTab:(int)index {     switch (index) {         case 1:                  self.tab1ViewController =[[tab1 alloc] initWithNibName:@"tab1" bundle:nil];              [self.view insertSubview:tab1ViewController.view belowSubview:tabbar1];             if (currentViewController != nil)                 [currentViewController.view removeFromSuperview];             currentViewController = tab1ViewController;              break;         case 2:                  self.tab2ViewController =[[tab2 alloc] initWithNibName:@"tab2" bundle:nil];            [self.view insertSubview:tab2ViewController.view belowSubview:tabbar1];            if (currentViewController != nil)                 [currentViewController.view removeFromSuperview];             currentViewController = tab2ViewController;                      break;         default:             break;     } } 

I added the the tab bar in interface builder.Can i do any thing in interface builder?

like image 580
Warrior Avatar asked Feb 24 '10 12:02

Warrior


People also ask

Where is tab bar in IOS?

The Tab Bar is the instance of the UITabBar class, which inherits UIView. The tab bar always appears across the bottom edge of the screen.

What is tab bar in IOS?

The tab bar interface displays tabs at the bottom of the window for selecting between the different modes and for displaying the views for that mode. This class is generally used as-is, but may also be subclassed. Each tab of a tab bar controller interface is associated with a custom view controller.


2 Answers

[tabBar setSelectedItem:[tabBar.items objectAtIndex:item.tag]]; 
like image 89
Sca Avatar answered Sep 21 '22 11:09

Sca


For swift if tabBar is @IBOutlet use in viewDidLoad:

tabBar.selectedItem = tabBar.items?.first 
like image 37
Mat0 Avatar answered Sep 23 '22 11:09

Mat0