0

我创建了一个按钮,并使用本指南将其放在标签栏上。

我希望仅当 selectedIndex 为 2 时才突出显示/选择此按钮。当 selectedIndex 为 0 1 3 4 时,我不希望其处于正常状态。这可能吗?

我尝试了以下操作,它突出显示了该按钮,但是一旦我单击该按钮,突出显示就会消失并且不会返回。

if (self.tabBarController.selectedIndex == 2) {
    button.selected = YES;
    button.highlighted = YES;
}else {      
    button.selected = NO;
    button.highlighted = NO;
}

如有必要,在标签栏上加载按钮的代码:

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect buttonFrame = button.frame;
buttonFrame.size.height = 55;
buttonFrame.size.width = 64;
buttonFrame.origin.x = 128;
buttonFrame.origin.y = 424;
button.frame = buttonFrame;
[button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"buttonhighlight.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:@"buttonhighlight.png"] forState:UIControlStateSelected];

[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

[_tabBarController.view addSubview:button];
4

2 回答 2

1

你在哪里调用你的按钮突出显示代码?我认为每次触摸选项卡时都需要调用它(实现知道用户何时切换选项卡的UITabBarControllerDelegate方法)。tabBarController:didSelectViewController:

例子:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 

    if([viewController.title isEqualToString:@"View controller 1"]) {

        NSLog(@"View controller 1 was selected");

    } else {

        NSLog(@"Some other view controller was selected");

     }
}
于 2012-06-06T21:12:01.937 回答
0

iv 将此添加到我的自定义 tabbar.m 文件中。

- (void)tabBar:(UITabBar *)theTabBar didSelectItem:(UITabBarItem *)item {
NSUInteger indexOfTab = [[theTabBar items] indexOfObject:item];
NSLog(@"Tab index = %u", indexOfTab);

if (indexOfTab == 0) {
    [button setSelected:false];
}
if (indexOfTab == 1) {
    [button setSelected:false];
}

if (indexOfTab == 3) {
        [button setSelected:false];
    }
if (indexOfTab == 4) {
        [button setSelected:false];

    }
}


- (void)buttonPressed:(id)sender {
        [button setSelected:true];

        [self setSelectedIndex:2];}

这对我有用,希望对其他人有帮助....

于 2012-11-01T23:13:16.787 回答