我在视图中遇到多个 UIButton 的问题。我希望单独选择按钮,一次选择多个按钮(例如:10 个按钮,选择按钮 1、4、5、9)。
在我的标头中,我有一个 IBOutletCollection 属性:
@property (retain, nonatomic) IBOutletCollection(UIButton) NSMutableArray *buttonToStaySelected;
在我的实现中,我有一个 IBAction:
-(IBAction)selectedButton:(id)sender{
for (UIButton *b in self.buttonToStaySelected) {
if (b.isSelected == 0){
[b setSelected:YES];
} else
[b setSelected:NO];
}
}
我遇到的问题是,当我选择与该集合关联的任何按钮时,它们都会更改为选定状态。我知道问题很可能(几乎肯定)存在于循环中,但是我尝试规定的每个条件都会破坏代码,并且没有一个按钮能够“更改”状态。
UPDATED
The selectButton:
发送的消息带有一个参数,该参数指定被点击的按钮,但您将该操作应用于集合中的所有按钮,而不仅仅是被点击的按钮。
-(IBAction)selectedButton:(id)sender
{
for (UIButton *b in self.buttonToStaySelected)
{
if (sender == b)
{
b.isSelected == !b.isSelected
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)