在Custom TabBar上按代码更改所选索引

时间:2013-10-12 18:58:07

标签: ios uitabbarcontroller uitabbar

我有一个自定义标签栏:

#import "CustomTabBar.h"

@implementation CustomTabBar

@synthesize btn0, btn1, btn2, btn3, btn4;

- (void)viewDidAppear:(BOOL)animated {

    [self HideTabBar];
    [self CustomTabBar];
}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
}

- (void)HideTabBar {

    for(UIView *view in self.view.subviews) {

        if([view isKindOfClass:[UITabBar class]]) {

            view.hidden = YES;
            break;
        }
    }
}

-(void)CustomTabBar {

    self.btn0 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn0.backgroundColor = [AppDelegate mainColor];
    btn0.frame = CGRectMake(0, 518, 60, 50);
    [btn0 setBackgroundImage:[UIImage imageNamed:@"fav_trans_tab"] forState:UIControlStateNormal];
    [btn0 setBackgroundImage:[UIImage imageNamed:@"fav_selected_tab"] forState:UIControlStateSelected];
    [btn0 setSelected:YES];
    [btn0 setTag:0];

    self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.backgroundColor = [AppDelegate mainColor];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"fav_trans_tab"] forState:UIControlStateNormal];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"fav_selected_tab"] forState:UIControlStateSelected];
    btn1.frame = CGRectMake(60, 518, 60, 50);
    [btn1 setTag:1];

    self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.backgroundColor = [AppDelegate mainColor];
    btn2.frame = CGRectMake(120, 518, 80, 50);
    [btn2 setBackgroundImage:[UIImage imageNamed:@"fav_trans_tab"] forState:UIControlStateNormal];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"fav_selected_tab"] forState:UIControlStateSelected];
    [btn2 setTag:2];

    self.btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.backgroundColor = [AppDelegate mainColor];
    btn3.frame = CGRectMake(200, 518, 60, 50);
    [btn3 setBackgroundImage:[UIImage imageNamed:@"fav_trans_tab"] forState:UIControlStateNormal];
    [btn3 setBackgroundImage:[UIImage imageNamed:@"fav_selected_tab"] forState:UIControlStateSelected];
    [btn3 setTag:3];

    self.btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn4.backgroundColor = [AppDelegate mainColor];
    btn4.frame = CGRectMake(260, 518, 60, 50);
    [btn4 setBackgroundImage:[UIImage imageNamed:@"fav_trans_tab"] forState:UIControlStateNormal];
    [btn4 setBackgroundImage:[UIImage imageNamed:@"fav_selected_tab"] forState:UIControlStateSelected];
    [btn4 setTag:4];


    // Add my new buttons to the view
    [self.view addSubview:btn0];
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    [self.view addSubview:btn3];
    [self.view addSubview:btn4];

    // Setup event handlers so that the buttonClicked method will respond to the touch up inside event.
    [btn0 addTarget:self action:@selector(TabClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 addTarget:self action:@selector(TabClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(TabClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn3 addTarget:self action:@selector(TabClicked:) forControlEvents:UIControlEventTouchUpInside];
    [btn4 addTarget:self action:@selector(TabClicked:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)TabClicked:(id)sender {

    [self SelectTab:[sender tag]];
}

- (void)SelectTab:(int)TabID {

    switch(TabID) {

        case 0: [btn0 setSelected:true]; [btn1 setSelected:false]; [btn2 setSelected:false]; [btn3 setSelected:false]; [btn4 setSelected:false]; break;
        case 1: [btn0 setSelected:false]; [btn1 setSelected:true]; [btn2 setSelected:false]; [btn3 setSelected:false]; [btn4 setSelected:false]; break;
        case 2: [btn0 setSelected:false]; [btn1 setSelected:false]; [btn2 setSelected:true]; [btn3 setSelected:false]; [btn4 setSelected:false]; break;
        case 3: [btn0 setSelected:false]; [btn1 setSelected:false]; [btn2 setSelected:false]; [btn3 setSelected:true]; [btn4 setSelected:false]; break;
        case 4: [btn0 setSelected:false]; [btn1 setSelected:false]; [btn2 setSelected:false]; [btn3 setSelected:false]; [btn4 setSelected:true]; break;
    }

    self.selectedIndex = TabID;
}

@end

当我单击标签栏时,我可以更改标签栏,但如果我按代码或按钮导航到标签栏索引,则标签栏选择的索引不会更改。我能做什么?如何在其他视图中访问SelectTab功能?

1 个答案:

答案 0 :(得分:0)

我只是调用 - (void)SelectTab:(int)TabID;每次我导航到一些视图。要从其他视图调用此方法,请使用this answer