为什么 viewWithTag 返回 nil
Why viewWithTag is returning nil
activite1Label为标签1
class StatsViewController: UIViewController {
@IBOutlet weak var activite1Label: UILabel!
@IBOutlet weak var activite2Label: UILabel!
@IBOutlet weak var activite3Label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
activite1Label.text = activite[0]
activite2Label.text = activite[1]
activite3Label.text = activite[2]
miseAjourTotal()
}
func miseAjourTotal() {
let leLabel = view.viewWithTag(1) as! UILabel
print("leLabel: \(leLabel.text)")
}
}
代码中没有任何内容表明标签具有标签 1。您应该转到故事板并检查标签是否具有标签 1 或以编程方式设置标签
activite1Label为标签1
class StatsViewController: UIViewController {
@IBOutlet weak var activite1Label: UILabel!
@IBOutlet weak var activite2Label: UILabel!
@IBOutlet weak var activite3Label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
activite1Label.text = activite[0]
activite2Label.text = activite[1]
activite3Label.text = activite[2]
miseAjourTotal()
}
func miseAjourTotal() {
let leLabel = view.viewWithTag(1) as! UILabel
print("leLabel: \(leLabel.text)")
}
}
代码中没有任何内容表明标签具有标签 1。您应该转到故事板并检查标签是否具有标签 1 或以编程方式设置标签