24

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Warrior
  • 39,156
  • 44
  • 139
  • 214
  • 2
    Why aren't you using the TabBarController again? – Beardo Feb 24 '10 at 16:37
  • 7
    Because TabBarController adds all sorts of headache. You get the convenience for a very hefty price. You can't add UITabBarControllers into navigation controllers. It's just ugly. Unfortunately, Apple gives no good documentation on creating just a simple UITabBar, and all the tutorials online uses XIBs not code.. ugly, ugly, ugly!!!! – Henley Jul 29 '11 at 02:17
  • Do you want to change the color of the image on the tab bar when selected? – Vineesh TP Jun 08 '12 at 08:45
  • 2
    @HenleyChiu "You can't add UITabBarControllers into navigation controllers": correct, but you can add a navigation controller to each of the UITabBarController's view controllers. If you think about the navigation of your app, it makes much more sense to have your view architecture setup this way. Given that switching tabs is a modal transition, it doesn't make sense to use the same navigation controller across different tabs -- what should happen to the navigation stack when you switch tabs? – dmur Jul 03 '14 at 18:53

9 Answers9

36
[tabBar setSelectedItem:[tabBar.items objectAtIndex:item.tag]];
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Sca
  • 377
  • 3
  • 2
  • 11
    this is wrong: "objectAtIndex:item.tag". The Tag can be in no relation to the index in the array. So, this only works if the tags are numbered identical to the indices of the array (e.g 0 - 4) – Toad Apr 18 '14 at 12:03
20

For swift if tabBar is @IBOutlet use in viewDidLoad:

tabBar.selectedItem = tabBar.items?.first
denis_lor
  • 6,212
  • 4
  • 31
  • 55
Mat0
  • 1,165
  • 2
  • 11
  • 27
10

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.

pheelicks
  • 7,461
  • 2
  • 45
  • 50
8

You can set the default index of the TabBarController by setting the selectedIndex property. This can be put in viewDidLoad or Before pushing the controller if you are doing it that way. This is done only when you are using a UITabBarController and Not just a UITabBar.

self.tabBarController.selectedIndex = 1

If you are using a TabBar without a TabBarController then you have to do it like this.

self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:1]
mehdigriche
  • 444
  • 1
  • 4
  • 14
Vineet Ashtekar
  • 1,952
  • 21
  • 16
3

if UITabBar is NOT already handled by a UITabBarController

[self.TabBar setSelectedItem:[[self.TabBar items] objectAtIndex:1]];

here TabBar is the Outlet for the UITabBar.

if UITabBar is already handled by a UITabBarController

[self.tabBarController setSelectedIndex:1];
Nikhil Augustine
  • 187
  • 2
  • 12
3

Swift 3:

@IBOutlet weak var uiTabBarOutlet: UITabBar!

uiTabBarOutlet.selectedItem = uiTabBarOutlet.items?[0] 
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
2

The following works perfectly for me in Swift 1.2

myTabBar.selectedItem = myTabBarItem

where myTabBar and myTabBarItem are IBOutlets to the respective elements on the storyboard.

mohonish
  • 1,396
  • 1
  • 9
  • 21
0

How I made it, using UITabBarDelegate:

#import "InfoSecurity.h"
#import "TabsCell.h"

@interface InfoSecurity () <UITabBarDelegate>

@property (strong, nonatomic) IBOutlet UITabBar *mainTab;

@property(weak,nonatomic) NSArray *TabArray;


@end

@implementation InfoSecurity

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

}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{

    if (_mainTab.selectedItem.tag == 1) {
        NSLog(@"TAB 1");
    }
    else if (_mainTab.selectedItem.tag == 2) {

        NSLog(@"TAB2");

    }
    else if (_mainTab.selectedItem.tag == 3)
    {
        NSLog(@"TAB3");
    }
    else
    {
        NSLog(@"TAB NOT WORKING");
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
bardzusny
  • 3,788
  • 7
  • 30
  • 30
alvin
  • 21
  • 1
0

Remember to set the delegate of the UITabBar in interface builder to be the view controller's class and also set <UITabBarDelegate> after the @interface declaration in the class.

you can then set the first tab to be highlighted as so:

- (void)viewDidLoad {
    [super viewDidLoad];

    if (tabBar.items.count >= 1) {
        [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
    }
}
Rob
  • 1,233
  • 13
  • 24