在Swift的UIView中找到UILabel [英] Find UILabel in UIView in Swift

查看:395
本文介绍了在Swift的UIView中找到UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在我的UIViewControllers的超级视图中找到我的UILabels。
这是我的代码:

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)
    }
}

这是如何在Objective-C中推荐它,但在Swift中我只得到UIViews和CALayer。我肯定在提供给此方法的视图中有UILabel。我缺少什么?

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?

我的UIViewController中的调用:

The call in my UIViewController:

  NightWatcher(view: self.view).watch("21:00", endTime: "08:30") // still working on


推荐答案

使用函数式编程概念可以更轻松地实现这一目标。

Using functional programming concepts you can achieve this much easier.

let labels = self.view.subviews.flatMap { $0 as? UILabel }

for label in labels {
//Do something with label
}

这篇关于在Swift的UIView中找到UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆