【问题标题】:Keep button selected or highlighted at specific tabbar selectedindex在特定标签栏选中或突出显示按钮 selectedindex
【发布时间】:2012-06-05 22:37:47
【问题描述】:

我创建了一个按钮,并将其放在标签栏上,并带有this guide

我希望仅当 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];

【问题讨论】:

    标签: iphone objective-c ios5 uibutton uitabbarcontroller


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您在哪里调用按钮突出显示代码?我认为您需要在每次触摸选项卡时调用它(实现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");
      
           }
      }
      

      【讨论】:

      • 我在application didFinishLaunchingWithOptions 中调用它,你能举个例子吗,一直试图让它工作,但是当我使用- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController1 { NSLog (@"VC1 selected");} 时,它会为我选择的每个视图控制器运行 nslog 代码。
      • 是的,它会显示相同的输出,因为您没有指定条件。我已经用一个例子编辑了我的答案。
      • 谢谢。接受了答案。我必须更改 我的代码 中的一些内容才能使其正常工作。也许这会对其他人有所帮助:它不起作用的第一个原因是我将名称 SecondViewController 更改为其他名称。出于某种原因,它无法接收viewController.title,即使其他一切正常。使用 xib 创建一个新文件并添加该文件可以解决该问题。它不起作用的其他原因:我使用按钮设置self.tabBarController.selectedIndex = 2,我必须删除按钮,为了获得突出显示的效果,我在条件发生变化时添加了一个图像子视图。
      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2010-12-11
      • 1970-01-01
      相关资源
      最近更新 更多