在Objective-C中循环遍历UIButtons数组

时间:2017-10-19 08:03:28

标签: objective-c arrays uibutton nsarray layer

我有一系列按钮,我想在所有按钮中更改一些属性。

阵列是这样的:

NSArray *buttons = @[_smallButton, _mediumButton, _largeButton, _xlargeButton];

按钮是插座:

@property (weak, nonatomic) IBOutlet UIButton *smallButton;
@property (weak, nonatomic) IBOutlet UIButton *mediumButton;
@property (weak, nonatomic) IBOutlet UIButton *largeButton;
@property (weak, nonatomic) IBOutlet UIButton *xlargeButton;

现在我想在数组的循环中更改所有borderColor并为每个设置标记:

for(int i=0; i< buttons.count; i++) {
   [buttons[i] layer].borderColor = [UIColor darkGrayColor].CGColor;
   [buttons[i] setTag:i];
}

关键是setTag工作正常并应用于所有按钮,但borderColor仅针对第一个项目进行更改,而不是全部更改。

borderColor only gets applied to the first button

有人知道我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

尝试为所有人设置borderWidthcornerRadius,即:

for(int i=0; i< buttons.count; i++) {
   [buttons[i] layer].borderColor = [UIColor darkGrayColor].CGColor;
   [buttons[i] layer].borderWidth = 1;
   [buttons[i] layer].cornerRadius = 4;
   [buttons[i] setTag:i];
}