【问题标题】:Variable UILabel name变量 UILabel 名称
【发布时间】:2020-04-29 11:48:25
【问题描述】:

是否可以将标签作为变量?我有多个标签:

@IBOutlet weak var FL1at0: UIButton!
@IBOutlet weak var FL1at3: UIButton!
@IBOutlet weak var FL1at6: UIButton!
@IBOutlet weak var FL1at9: UIButton!
@IBOutlet weak var FL1at12: UIButton!
@IBOutlet weak var FL1at15: UIButton!
@IBOutlet weak var FL1at18: UIButton!
@IBOutlet weak var FL1at21: UIButton!

示例函数:

       else
       {
           UserDefaults.standard.set(true, forKey: "RedFl1")
           self.FL1at0.setTitleColor(UIColor.red, for: .normal); self.ProgressUpdate()
       }

有没有办法以这样的方式设置表格:

self.FL1at"\(TimeStamp)".setTitleColor(UIColor.red, for: .normal)//TimeStamp comes from function

【问题讨论】:

    标签: swift variables uibutton uilabel


    【解决方案1】:

    试试这个:

    let dict: Dictionary<String, UIButton> = ["FL1at0": FL1at0, "FL1at3": FL1at3, ...]
    let x = "FL1at\(TimeStamp)"
    if let y = dict[x] as? UIButton {
        y.setTitleColor(UIColor.red, for: .normal)
    }
    

    【讨论】:

      【解决方案2】:

      你不能。 但是您可以将按钮保存在字典中:

      var dic: [String: UIButton] = ["FL1at0": FL1at0]
      

      然后你可以使用 name 来访问它:

      dic["FL1at\(TimeStamp)"]?.setTitleColorxxxx..
      

      【讨论】:

        猜你喜欢
        • 2011-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-29
        • 1970-01-01
        • 2013-04-28
        • 1970-01-01
        相关资源
        最近更新 更多