IBOutlet收集UIButtons - 更改按钮的选定状态 [英] IBOutletCollection of UIButtons - changing selected state of buttons

查看:117
本文介绍了IBOutlet收集UIButtons - 更改按钮的选定状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图中遇到多个UIButton的问题。我希望单独选择按钮,一次选择多个(例如:10个按钮,选择按钮1,4,5,9)。

I'm having an issue with multiple UIButtons in a view. I'd like the buttons to be selected individually, with multiple selected at a time (example: 10 buttons, with buttons 1, 4, 5, 9 selected).

在我的标题中,我有一个IBOutletCollection的属性:

In my header I have a property for the IBOutletCollection:

@property (retain, nonatomic) IBOutletCollection(UIButton) NSMutableArray *buttonToStaySelected;

在我的实施中,我有一个IBAction:

In my implementation, I have an IBAction:

-(IBAction)selectedButton:(id)sender{
  for (UIButton *b in self.buttonToStaySelected) {
     if (b.isSelected == 0){
        [b setSelected:YES];
  } else
        [b setSelected:NO];
  }
}

我遇到的问题是当我选择任何与集合相关的按钮,它们都变为选中状态。我知道最有可能(几乎可以肯定)的问题在于循环,但是我试图规定的每个条件都会破坏代码,并且没有任何按钮能够改变状态。

The issue I'm having is when I select any of the buttons tied to the collection, they all change to selected. I know the problem most likely (almost certain) lies in the loop, but every condition I've tried to stipulate breaks the code and leaves none of the buttons able to "change" state.

更新

UPDATED

要让它们可选,更改状态并检查多个,我用过这个作为我的最终代码:

To have them selectable, change state and check off multiple, I used this as my final code:

-(IBAction)selectedButton:(id)sender {
  for (UIButton *b in self.buttonToStaySelected) {
      if (sender == b) {
      [b setSelected:!b.isSelected];
    }
   }
  }

感谢所有帮助!

推荐答案

selectButton:消息与参数一起发送,该参数指定已点按的按钮,但您将操作应用于集合中的所有按钮,而不仅仅是已点按的按钮。

The selectButton: message is sent with an argument which specifies the button that was tapped, but you apply the action to all buttons in the collection, not just the button that was tapped.

-(IBAction)selectedButton:(id)sender
{
  for (UIButton *b in self.buttonToStaySelected)
  {
     if (sender == b)
     {
        b.isSelected == !b.isSelected
     }
  }
}

这篇关于IBOutlet收集UIButtons - 更改按钮的选定状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆