Reputation: 555
I have many subviews (button,label,textField)inside view.These are dynamically created depending on the xml
elements. if xml
elements are 13, the total button will also be 13 buttons. i know how to search all subviews inside view and which subviews is UIButton
or UITextField
by the following code.
for subView in view.subviews {
if subView is UILabel {
}
else if subView is UITextField {
}
}
what i want to know is how can i get the button's tag and title and textField's text?
Upvotes: 3
Views: 1103
Reputation: 2540
You should write this :
for subView in self.view.subviews
{
if let textField = subView as? UITextField
{
let textFieldText = textField.text
print(textFieldText!)
}
else if let Label = subView as? UILabel
{
let labelText = Label.text
print(labelText!)
}
}
Upvotes: 0
Reputation: 2693
Try this code:
for subView in view.subviews {
if subView is UILabel {
let label = subView as! UILabel
let labelText = label.text
}
else if subView is UITextField {
let textField = subView as! UITextField
let textFieldText = textField.text
}
}
Upvotes: 2