UIView viewwithtag method in swift [closed]

2019-03-23 08:25发布

I'm trying to learn some swift. I programmatically add labels. I want to change their properties later.

the viewwithtag method returns a UIView, how to I access my UILabel from this?

cheers

3条回答
对你真心纯属浪费
2楼-- · 2019-03-23 09:13

You need to write as

var getMyLabel : UILabel = self.view.viewWithTag(tagValue) as UILabel;
查看更多
成全新的幸福
3楼-- · 2019-03-23 09:18

viewWithTag: returns a UIView, you need to typecast it to UILabel.

var yourLabel : UILabel = yourView.viewWithTag(yourTag) as! UILabel;
查看更多
孤傲高冷的网名
4楼-- · 2019-03-23 09:19

You need to use a typecast. This code will do it:

    if let theLabel = self.view.viewWithTag(123) as? UILabel {
        theLabel.text = "some text"
    }
查看更多
登录 后发表回答