How to loop through all UIButtons in my Swift view?

前端 未结 9 1231
抹茶落季
抹茶落季 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:18

    This code seems to be quite useful for iterating over any object within a view, just change UIButton for any other subview type such as UIView or UIImageView, etc.

    let filteredSubviews = self.view.subviews.filter({
        $0.isKindOfClass(UIButton)})
    
    for view in filteredSubviews {
        //Do something        
    }
    

提交回复
热议问题