views:

1075

answers:

4

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?

Please help me out.Thanks.

+3  A: 

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
This will load the view , but the tab bar item will not be selected.(appearance of item 1 tab bar)
Warrior
The second line does the job for me. Maybe you need to clarify what you want to happen.This code works for me:UITabBar *tabBar_; ... NSArray *tabBarItems = tabBar_.items; [tabBar_ setSelectedItem:[tabBarItems objectAtIndex:0]];
iphone007
A: 

Simply set the selectedIndex property of your UITabBarController to the selected tab

tabBarController.selectedIndex = 1;
AlexVogel
I am using tab bar and not tab bar controller
Warrior
A: 

[tabBar setSelectedItem:[tabBar.items objectAtIndex:item.tag]];

Sca
A: 

I want to show image on tabbar item,

[tabbarItem setImage:[UIImage imageNamed:@"setimage.png"]];

but the image is not displaying on the tab bar item, it shows grey colored folder like image, when i select that tab bar item it turns blue highlighted image,

but it is not showing the image what i used.

how to do this????

thank you

sudheer

sudheer