【问题标题】:Loop over an array of UIButtons in Objective-C在 Objective-C 中循环 UIButtons 数组
【发布时间】:2017-10-19 08:03:27
【问题描述】:

我有一组按钮,我想更改所有按钮的一些属性。

数组是这样的:

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;

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

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

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

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

【问题讨论】:

    标签: objective-c arrays uibutton nsarray layer


    【解决方案1】:

    尝试为所有这些设置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];
    }
    

    【讨论】:

    • 哦,这么严重的错误,问题是borderWidth,我在storyboard中设置了第一个按钮borderWidth,其余的忘记设置了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    相关资源
    最近更新 更多