Ios 对于具有UIButton名称的循环
我需要在编号数组中的多个按钮上设置默认值。现在我使用的是if语句,但它太长了,必须有一个更好的方法,也许有一个循环 当前代码Ios 对于具有UIButton名称的循环,ios,for-loop,uibutton,Ios,For Loop,Uibutton,我需要在编号数组中的多个按钮上设置默认值。现在我使用的是if语句,但它太长了,必须有一个更好的方法,也许有一个循环 当前代码 @interface HelpPage() @property (weak, nonatomic) IBOutlet UIButton *box0; @property (weak, nonatomic) IBOutlet UIButton *box1; @property (weak, nonatomic) IBOutlet UIBut
@interface HelpPage()
@property (weak, nonatomic) IBOutlet UIButton *box0;
@property (weak, nonatomic) IBOutlet UIButton *box1;
@property (weak, nonatomic) IBOutlet UIButton *box2;
@property (weak, nonatomic) IBOutlet UIButton *box3;
@property (weak, nonatomic) IBOutlet UIButton *box4;
@property (weak, nonatomic) IBOutlet UIButton *box5;
// I have over 120 buttons on this screen
@end
@implementation Help
NSMutableArray *boxes = [NSMutableArray arrayWithArray:[defaults objectForKey:@"helpboxeschecked"];
NSInteger i;
for (i=0; i < boxes.count; i++) {
NSString *box = boxes[i];
if ([box isEqualToString:@"0"]) {
[_box0 setImage:[UIImage imageNamed:@"checked.png"] forState: UIControlStateNormal];
}
if ([box isEqualToString:@"1"]) {
[_box1 setImage:[UIImage imageNamed:@"checked.png"] forState: UIControlStateNormal];
}
if ([box isEqualToString:@"2"]) {
[_box2 setImage:[UIImage imageNamed:@"checked.png"] forState: UIControlStateNormal];
}
// and so on for all boxes
}
// **Isn't there a way to have it be**
for (i=0; i < boxes.count; i++) {
if ([box[i] = i) {
[_box[i] setImage:[UIIMage imageNamed:@"checked.png"] forState: UIControlStateNormal];
}
}
@界面帮助页()
@属性(弱,非原子)IBUIButton*box0;
@属性(弱,非原子)IBUIButton*box1;
@属性(弱、非原子)IBUIButton*box2;
@属性(弱、非原子)IBUIButton*box3;
@属性(弱、非原子)IBUIButton*box4;
@属性(弱、非原子)IBUIButton*box5;
//我在这个屏幕上有120多个按钮
@结束
@实施帮助
NSMutableArray*Box=[NSMutableArray arrayWithArray:[默认值objectForKey:@“helpboxeschecked”];
恩森特格尔一世;
对于(i=0;i
您可以将按钮添加到iOutletCollection
中,并在按钮上设置标记,然后执行以下操作:
...
for (UIButton *button in myButtonCollection) {
if (button.tag == box.intValue) {
// set image
}
}
或者只需将它们添加到数组中:
NSArray *buttons = @[_box1, _box2 ...
我会试试的。谢谢!