簡體   English   中英

如何在iphone中默認選擇標簽欄項目1?

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

我是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;
    }
}

我在界面構建器中添加了標簽欄。我可以在界面構建器中執行任何操作嗎?

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

對於swift,如果tabBar是@IBOutlet在viewDidLoad中使用:

tabBar.selectedItem = tabBar.items?.first

您是否只能在顯示視圖時調用方法選擇選項卡? 像這樣:

[self activateTab:1];

要更改選擇了哪個標簽欄項,請使用:

[myTabBar setSelectedItem:myTabBarItem];

其中myTabBarItem是相關視圖的UITabBarItem實例。

您可以通過設置selectedIndex屬性來設置TabBarController的默認索引。 這可以放在viewDidLoad中或者在推送控制器之前如果你這樣做的話。 僅當您使用TabBarController而不僅僅是TabBar時才會執行此操作。

tabBarController.selectedIndex = 1;

如果您使用的是沒有TabBarController的TabBar,那么您必須這樣做。

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

如果UITabBar尚未由UITabBarController處理

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

這里TabBar是UITabBar的Outlet。

如果UITabBar已經由UITabBarController處理

[self.tabBarController setSelectedIndex:1];

以下在Swift 1.2中完美適合我

myTabBar.selectedItem = myTabBarItem

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

斯威夫特3:

@IBOutlet weak var uiTabBarOutlet: UITabBar!

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

請記住將接口構建器中的UITabBar的委托設置為視圖控制器的類,並在類中的@interface聲明之后設置<UITabBarDelegate>

然后,您可以將第一個選項卡設置為突出顯示為:

- (void)viewDidLoad {
    [super viewDidLoad];

    if (tabBar.items.count >= 1) {
        [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM