UIView viewwithtag method in swift [closed]

孤人 提交于 2020-01-01 04:08:07

问题


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


回答1:


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

    if let theLabel = self.view.viewWithTag(123) as? UILabel {
        theLabel.text = "some text"
    }



回答2:


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

var yourLabel : UILabel = yourView.viewWithTag(yourTag) as! UILabel;



回答3:


You need to write as

var getMyLabel : UILabel = self.view.viewWithTag(tagValue) as UILabel;


来源:https://stackoverflow.com/questions/24096908/uiview-viewwithtag-method-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!