在 Swift 中以编程方式访问和更新堆栈视图中的 label.text 或其他 UILabel 属性

Access and update label.text or other UILabel properties inside a stackview programatically in Swift

我在 Swift 中以编程方式创建了一个带有标签的堆栈视图。但是,我试图找出如何以编程方式更新标签? (我没有使用故事板或 IBOutlets)

  let LabelStack: UIStackView = {
      
        let label1: UILabel = {
            let label = UILabel()
            label.text = "Label 1"
            label.font = .systemFont(ofSize: 14, weight: .bold)
            label.numberOfLines = 0
            label.backgroundColor = .clear
            label.textAlignment = .left
            label.sizeToFit()
            return label
        }()
        
        let label2: UILabel = {
            let label = UILabel()
            label.text = "Label 2"
            label.font = .systemFont(ofSize: 14, weight: .bold)
            label.numberOfLines = 0
            label.backgroundColor = .clear
            label.textAlignment = .left
            label.sizeToFit()
            return label
        }()
        
        let stack = UIStackView(arrangedSubviews: [label1, label2])
        stack.distribution = .equalSpacing
        stack.spacing = 4.0
        return stack
    }()

尝试使用函数更新标签文本时,我不确定如何访问标签属性来进行此更改。通常,对于在堆栈外创建的标签,我可以简单地使用:

func updateLabel() {
    label1.text = "Updated Label 1 text"
    label2.text = "Updated Label 2 text" 
}

使用什么语法来访问位于带有标签的 UIStackview 中的这些标签属性?

你可以在外面制作它们let labelStack: UIStackView = {

    let label1: UILabel = {
        let label = UILabel()
        label.text = "Label 1"
        label.font = .systemFont(ofSize: 14, weight: .bold)
        label.numberOfLines = 0
        label.backgroundColor = .clear
        label.textAlignment = .left
        label.sizeToFit()
        return label
    }()
    
    let label2: UILabel = {
        let label = UILabel()
        label.text = "Label 2"
        label.font = .systemFont(ofSize: 14, weight: .bold)
        label.numberOfLines = 0
        label.backgroundColor = .clear
        label.textAlignment = .left
        label.sizeToFit()
        return label
    }()

let labelStack: UIStackView = { 
    let stack = UIStackView(arrangedSubviews: [label1, label2])
    stack.distribution = .equalSpacing
    stack.spacing = 4.0
    return stack
}()

或者这样做

if let label1 = labelStack.arrangedSubviews.first as? UILabel {
  // proceed
}
if let label2 = labelStack.arrangedSubviews.last as? UILabel {
  // proceed
}

您可以在外部创建它们。但这需要您稍后添加 arrangedSubviews,因为它尚未初始化。所以你可以让 stackView Lazy 等待 init 成为 运行:

let label1: UILabel = {
    let label = UILabel()
    label.text = "Label 1"
    label.font = .systemFont(ofSize: 14, weight: .bold)
    label.numberOfLines = 0
    label.backgroundColor = .clear
    label.textAlignment = .left
    label.sizeToFit()
    return label
}()

let label2: UILabel = {
    let label = UILabel()
    label.text = "Label 2"
    label.font = .systemFont(ofSize: 14, weight: .bold)
    label.numberOfLines = 0
    label.backgroundColor = .clear
    label.textAlignment = .left
    label.sizeToFit()
    return label
}()


lazy var labelStack: UIStackView = {
    let stack = UIStackView(arrangedSubviews: [label1, label2])
    stack.distribution = .equalSpacing
    stack.spacing = 4
    return stack
}()

如果您必须静态地标记和 2 个文本,您可以这样做:

   zip(labelStack.arrangedSubviews, ["textupdate1", "textupdate2"]).forEach { (element value) in
        (element as? UILabel)?.text = value
    }