如何将属性设置为包含“UIButtons”的“IBOutletCollection”的“扩展”?我正在尝试以与扩展 UIButton 类似的方式来执行此操作,这似乎并未扩展到 UIButton 的集合。我希望尽可能彻底和清晰。
虽然有类似的问题:
1.更改IBOutletCollection中UIButton的颜色
2.在IBOutletCollection中设置UIButtons的单独标题
...以及其他许多人。我仍然没有掌握如何做到这一点,并且可以真正使用你们任何人都可以提供的帮助。
这是我到目前为止收集的内容:
- IBOutletCollection 始终是 NSArray。
- 使用 for-in-loop 遍历数组的内容。
这是我的代码示例:(我尝试用类似于“Array”的“Collection”替换“Array”所在的位置:“NSArray”和“Collection”)
//Inside the main viewController:
@IBOutlet var ButtonCollection: [UIButton]!
//Inside the extention file:
extension Array where Element == UIButton
{
func sharedButtonProperties()
{
for button in ButtonCollection
{
self.setTitleColor(.red, for: .normal)
//MARK: More properties will go here, once I get this to work.
}
}
}
//calling the code inside main viewController, within "viewDidAppear"
ButtonCollection.sharedButtonProperties()
现在这是一个 UIButton 的单个 IBOutlet 的扩展示例,它工作得很好,但不适用于 Collection:
//Inside the main viewController:
@IBOutlet weak var ButtonSingle: UIButton?
//Inside the extention file:
extension UIButton
{
func buttonProperties()
{
self.setTitleColor(.red, for: .normal)
}
}
//calling the code inside main viewController, within "viewDidAppear"
ButtonSingle?.buttonProperties()
我肯定不是专家编码器,非常感谢提供的任何帮助。
谢谢你。