[英]Enabling and disabling multiple buttons in view issue
我有12个按钮的视图。 我希望能够在单击动作按钮并将其设置为禁用时仅执行一次该按钮。 当我点击另一个按钮时,我希望发生相同的事情+将其他按钮设置为启用。 有解决方案吗?
您可以给每个按钮一个标签,当使用viewWithTag
方法点击一个按钮时,您可以启用和禁用每个按钮:
创建UIButton
,可以添加:
UIButton *button0 = ...
button0.tag = 0;
UIButton *button1 = ...
button1.tag = 1;
//and so on
在按钮的每个操作中,您都必须像这样传递id对象:
-(void)tapButtonOne:(id)sender {
//with this sender you can retrive the tag of the button clicked
UIButton *button = sender;
int buttonTag = button.tag
//now you can check every button and enable the other that haven't the same buttonTag
//with the first tag = 0 plus 12 the last tag will be 11 so i<12
for (int i = 0; i<12; i++) {
//self.view if you have added the buttons on self.view, otherwise you must write your view
UIButton *buttonTemp = (UIButton *)[self.view viewWithTag:i];
if(buttonTemp.tag != buttonTag) {
buttonTemp.enabled = YES;
}
else {
buttonTemp.enabled = NO;
}
}
}
代替创建插座,而是创建IBOutletCollection
。
从界面构建器拖动到应用程序时,可以将“插座”选择更改为“插座集合”。
完成第一个按钮的操作后,按住Control键拖动所有按钮并将其链接到同一集合。
将所有按钮连接到同一IBAction,并确保它指定了发送者(这是默认设置)。 然后,在您的操作中:
for (UIButton* button in self.yourOutletCollection){
if (button == sender){
// this is the button that was tapped
} else {
// all the other buttons go here
}
}
请享用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.