ios swift中的 UIView viewwithtag 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24096908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIView viewwithtag method in swift
提问by tchnvkng
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?
viewwithtag 方法返回一个 UIView,如何从中访问我的 UILabel?
cheers
干杯
回答by Adam
You need to use a typecast. This code will do it:
您需要使用类型转换。这段代码会做到:
if let theLabel = self.view.viewWithTag(123) as? UILabel {
theLabel.text = "some text"
}
回答by Midhun MP
viewWithTag:returns a UIView, you need to typecast it to UILabel
.
viewWithTag:返回一个 UIView,您需要将其类型转换为UILabel
.
var yourLabel : UILabel = yourView.viewWithTag(yourTag) as! UILabel;
回答by iPatel
You need to write as
你需要写成
var getMyLabel : UILabel = self.view.viewWithTag(tagValue) as UILabel;