Swift - Retrieving subviews
For find all subviews from your view you can use this code:
for subview in self.view.subviews {
// Use your subview as you want
}
But for use it, you must identify view what you need. You can mark any element, what you create, with special Identifier like this:
myButton.restorationIdentifier = "mySpecialButton";
And after that you can find your element use this structure:
for view in view.subviews {
if (view.restorationIdentifier == "mySpecialButton") {
print("I FIND IT");
view.removeFromSuperview();
}
}
:)
UIViewController
doesn't have a subviews
property. It has a view
property, which has a subviews
property:
for subview in self.view.subviews {
// Manipulate the view
}
But typically this is not a good idea. You should instead put the labels you want into an IBOutletCollection
and iterate over that. Otherwise you've very tied to the exact set of subviews (which may change).
To create the IBOutletCollection
, select all the labels you want in IB, and control-drag them to the source code. It should ask if you want to make a collection array. (Note that there is no promise on the order of this array.)