我有一个项目有 35 个按钮:
IBOutlet UIButton *button1;
IBOutlet UIButton *button2;
IBOutlet UIButton *button3;
...
IBOutlet UIButton *button35;
在我的例子中,我正在创建一个函数,它将从 0-35 中选择一个数字,并且我试图根据生成的数字选择按钮,如下所示:
int x = arc4random() % 35;
button[x].layer.borderColor = [[UIColor darkGrayColor] CGColor];
但是代码不起作用,因为我认为我无法选择按钮,我该如何解决这个问题并选择按钮并更改边框颜色?
最佳答案
我建议从某个固定偏移量开始为您的按钮分配顺序标签,然后使用 viewWithTag 来获取按钮,如 DanielM 的替代建议中所建议的那样。
#define K_TAG_BASE 100 //BUTTON TAGS START AT 100
int tag = arc4random() % 35 + K_TAG_BASE;
NSButton aButton = [self.view viewWithTag: tag];
aButton.layer.borderColor = [[UIColor darkGrayColor] CGColor];
关于ios - 选择带有数字的特定 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26111424/