I'm trying to find my UILabels in my superview of my UIViewControllers. This is my code:
func watch(startTime:String, endTime:String) {
if superview == nil {println("NightWatcher: No viewcontroller specified");return}
listSubviewsOfView(self.superview!)
}
func listSubviewsOfView(view: UIView) {
var subviews = view.subviews
if (subviews.count == 0) { return }
view.backgroundColor = UIColor.blackColor()
for subview in subviews {
if subview.isKindOfClass(UILabel) {
// do something with label..
}
self.listSubviewsOfView(subview as UIView)
}
}
This is how it is recommended to in Objective-C, but in Swift I get nothing but UIViews and CALayer. I definitely have UILabels in the view that is supplied to this method. What am I missing?
The call in my UIViewController:
NightWatcher(view: self.view).watch("21:00", endTime: "08:30") // still working on
Swift 4
Adepting mKane's answer you can use this code:
You could set a
tag
to yourUILabel
in the Storyboard or programmatically using:Then, to find it use:
let myLabel = view.viewWithTag(1234)
Using functional programming concepts you can achieve this much easier.
Here's a version that will return an
Array
of all theUILabel
views in whatever view you pass in:Then you can iterate over them to do whatever you'd like: