简体   繁体   中英

How to loop through all UIButtons in my Swift view?

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.

for btns in self.view as [UIButton] {
  // set the title to ""
}

This code should work:

for view in self.view.subviews as [UIView] {
    if let btn = view as? UIButton {
        btn.setTitleForAllStates("")
    }
}

You need to iterate through the subViews array.

Shortened and updated for Swift 3 & 4

for case let button as UIButton in self.view.subviews {
    button.setTitleForAllStates("")
}

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
}

To add some context for a common use case, suppose the buttons were in a scroll view and you wanted to highlight the tapped button and de-highlight the other buttons. In this situation, you would direct all buttons to one action method:

@objc private func buttonAction(_ button: UIButton) {
    for case let b as UIButton in view.scrollView.subviews {
        if b == button {
            b.setTitleColor(UIColor.green, for: []) // highlight
        } else {
            b.setTitleColor(UIColor.black, for: []) // de-highlight
        }
    }
}

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        
}

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)
        }
    }

Swift 4:

let subviewButtons = self.view.subviews.filter({$0.isKind(of: UIButton.self)})

for button in subviewButtons {
    //do something        
}

If you have UIView 's within self.view then you need to loop through the subviews while searching for UIButton . Using the accepted answer, I made this little function to do so:

Swift 4 + :

func findButton(`in` view: UIView){
    for view in view.subviews as [UIView] {
        if let button = view as? UIButton {
            // Do something with 'button'
        }else{
            // Loop through subview looking for buttons
            findButton(in: view)
        }
    }
}

Usage:

override func viewDidLoad() {
    findButton(in: self.view)
}

Hope this helps!

Here's a short way in Swift if you know the subview only has buttons:

myView.subviews.map {
  ($0 as? UIButton)!.enabled = false
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM