How to loop through all UIButtons in my Swift view?

前端 未结 9 1247
抹茶落季
抹茶落季 2021-02-05 05:22

How would I loop through all UIButtons in my view in Swift? I would want to set all the titles to \"\", but my for-loop in Swift is giving an error.

9条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 06:20

    Looping over subview works, but it's sometimes a little ugly, and has other issues.

    If you need to loop over some specific buttons, for example to add corner radius or change tint or background color, you can use an array of IBOutlets and then loop over that.

    var buttons = [SkipBtn, AllowBtn]
    for button in buttons as! [UIButton] {
        button.layer.cornerRadius = 5
    }
    

提交回复
热议问题