变量 UILabel 名称

Variable UILabel name

能不能把标签做成变量?我有多个标签:

@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()
       }

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

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

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

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

然后您可以使用名称访问它,例如:

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

试试这个:

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)
}