如何在iphone中默认选择标签栏项目1?

问题描述 投票:17回答:9

我是iPhone开发的新手。我正在创建基于视图的应用程序。我在视图中添加了一个标签栏(而不是标签栏控制器)。通过将标签栏项目的标签值设置为1,2,我已在tabbar项目单击事件上加载了每个标签栏的视图。

我希望默认选中标签栏1。我该怎么办?

这是我的代码:

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

我在界面构建器中添加了标签栏。我可以在界面构建器中执行任何操作吗?

iphone uitabbar default-selected
9个回答
36
投票
[tabBar setSelectedItem:[tabBar.items objectAtIndex:item.tag]];

12
投票

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

tabBar.selectedItem = tabBar.items![0]

9
投票

您是否只能在显示视图时调用方法选择选项卡?像这样:

[self activateTab:1];

要更改选择了哪个标签栏项,请使用:

[myTabBar setSelectedItem:myTabBarItem];

其中myTabBarItem是相关视图的UITabBarItem实例。


6
投票

您可以通过设置selectedIndex属性来设置TabBarController的默认索引。这可以放在viewDidLoad中或者在推送控制器之前如果你这样做的话。仅当您使用TabBarController而不仅仅是TabBar时才会执行此操作。

tabBarController.selectedIndex = 1;

如果您使用的是没有TabBarController的TabBar,那么您必须这样做。

self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:1];

3
投票

如果UITabBar尚未由UITabBarController处理

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

这里TabBar是UITabBar的Outlet。

如果UITabBar已经由UITabBarController处理

[self.tabBarController setSelectedIndex:1];

2
投票

以下在Swift 1.2中完美适合我

myTabBar.selectedItem = myTabBarItem

其中myTabBar和myTabBarItem是故事板上各个元素的IBOutlets。


2
投票

斯威夫特3:

@IBOutlet weak var uiTabBarOutlet: UITabBar!

uiTabBarOutlet.selectedItem = uiTabBarOutlet.items?[0] 

0
投票

我是如何使用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.
}

0
投票

记得在界面构建器中将UITabBar的委托设置为视图控制器的类,并在类中的<UITabBarDelegate>声明之后设置@interface

然后,您可以将第一个选项卡设置为突出显示为:

- (void)viewDidLoad {
    [super viewDidLoad];

    if (tabBar.items.count >= 1) {
        [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.