简体   繁体   中英

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

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?

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

对于swift,如果tabBar是@IBOutlet在viewDidLoad中使用:

tabBar.selectedItem = tabBar.items?.first

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.

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 TabBarController and Not just a TabBar.

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];

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];

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.

Swift 3:

@IBOutlet weak var uiTabBarOutlet: UITabBar!

uiTabBarOutlet.selectedItem = uiTabBarOutlet.items?[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.
}

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]];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM