我创建了一个 .xib 文件,其中包含一堆标签,可与 SwipeView 一起使用。 。我的问题是我无法/不知道如何在加载 View 后访问和更改其文本:
let newView = NSBundle.mainBundle().loadNibNamed("SwipeHelperView", owner: self, options: nil)[0] as UIView
for (var subview) in newView.subviews
{
if (subview.isKindOfClass(UILabel) && subview.tag == 0)
{
subview.text = "asdaS" //ERROR: cannot assign 'text' in 'subview'
}
}
当场创建标签会更容易吗,如下所示:
var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(0, 0)
label.text = "some text"
newView.addSubview(label)
而不是尝试从“newView”UIView 访问它们?有“推荐”方式还是与偏好有关?
最佳答案
你应该转换它:
let newView = NSBundle.mainBundle().loadNibNamed("SwipeHelperView", owner: self, options: nil)[0] as UIView
for subview in newView.subviews as [UIView]
{
if subview.tag == 0
{
if let label = subview as? UILabel {
label.text = "asdaS"
}
}
}
如果您设置自定义标签(例如 333),您可以获得它:
if let label = newView.viewWithTag(333) as? UILabel {
label.text = "TEST"
}
关于ios - 尝试从 UIView 访问标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26232170/