0

I have some buttons on the view. I don't know how many, I know that it can be minimum of 2 or maximum of 4. I want to check each button's title text and add to array true or false depending on value of title.

is it possible to do ?

Alexey K
  • 6,537
  • 18
  • 60
  • 118
  • possible duplicate of http://stackoverflow.com/questions/26086175/swift-retreiving-subviews my previous flag was for objective-C which you could argue is wrong. – Orion Aug 03 '15 at 11:20

2 Answers2

1

Yes, it is possible to iterate through all objects inside the view. To see all the subviews present in your view, you can do following:

    for view in self.view.subviews as! [UIView] {
    if let btn = view as? UIButton {
        if btn.title == "whateverYourCriteriaIs" { 
            //your code
        }
        else {
           //do something else
        }
    }
}
Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40
Munahil
  • 2,381
  • 1
  • 14
  • 24
1

Try below thing..

    for view in self.view.subviews {
        if view.isKindOfClass(UIButton){
            // Add you logic over here. 
            // you can check the tag of button as well.
        }
    }
Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40