0

我有一个自定义标签栏:

#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 功能?

4

1 回答 1

0

我只是调用 -(void)SelectTab:(int)TabID; 每次我导航到某个视图。要从其他视图调用此方法,我使用此答案

于 2013-10-14T15:51:56.127 回答