如何批量设置IBOutlet集合的属性?

时间:2022-05-03 16:59:03

I created an IBOutlet collection of 4 buttons named buttonCollection, I need to set the properties of every button like this:

我创建了一个名为buttonCollection的4个按钮的IBOutlet集合,我需要设置每个按钮的属性,如下所示:

button.layer.borderWidth = 1
button.layer.borderColor = UIColor.white.cgColor
button.layer.cornerRadius = 10

Since buttonCollection is an array of UIButtons, I can set the properties one by one, but that'll result in huge amount of code, especially when the buttons gets more, is there a way to set them all at a time?

由于buttonCollection是一个UIButtons数组,我可以逐个设置属性,但这会产生大量的代码,特别是当按钮变得更多时,有没有办法一次设置它们?

2 个解决方案

#1


1  

for button in buttonCollection {
   //do something on button
}

#2


1  

You can use property observers.

您可以使用属性观察者。

@IBOutlet var buttons: [UIButton]!{
    didSet{
        for button in buttons{
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor.whiteColor().CGColor
            button.layer.cornerRadius = 10
            button.layer.masksToBounds = true
        }
    }
}

#1


1  

for button in buttonCollection {
   //do something on button
}

#2


1  

You can use property observers.

您可以使用属性观察者。

@IBOutlet var buttons: [UIButton]!{
    didSet{
        for button in buttons{
            button.layer.borderWidth = 1
            button.layer.borderColor = UIColor.whiteColor().CGColor
            button.layer.cornerRadius = 10
            button.layer.masksToBounds = true
        }
    }
}