How to loop through all UIButtons in my Swift view?

前端 未结 9 1410
抹茶落季
抹茶落季 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 05:57

    Used some of the offered questions out there and created my own. I believe is the most efficient when you want to programmatically set up the title of various UIButtons(in my case I am building a quiz)

    By randomising my array list and with just a for loop I printing the item at index to the button title

    for view in self.viewForButtons.subviews{
            if view.isKindOfClass(UIButton)
            {
                let button : UIButton = view as! UIButton
                button.setTitle("item[i]", forState: .Normal)
            }
        }
    

提交回复
热议问题