gpt4 book ai didi

ios - 试图从 UIView 访问标签

转载 作者:可可西里 更新时间:2023-11-01 01:06:41 26 4
gpt4 key购买 nike

我创建了一个 .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/

26 4 0