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.
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!