当从print(mess)
函数调用viewDidLoad
时,它将打印控制台中期望的内容,但是当将其分配给UITextField
或UILabel
时,分配的值不会反映在UITextField
中或UILabel
。为什么是这样 ?
这样做:
var mess: String = ""
override func viewDidLoad()
{
super.viewDidLoad()
self.infoLabel.text = mess //label not getting updated
print(mess) //printing the required output in console
}
但是当我这样做时,它工作正常
override func viewDidLoad()
{
super.viewDidLoad()
let date = DATE()
let calendar = Calendar.current
let minutes = calendar.component(.minutes, from: date)
temp.text = String(minutes)
}
最佳答案
首先将值分配给mess变量,然后更新UITextField值。
在viewDidLoad
中为UITextField分配值时,其默认值为""
。
var mess: String = ""
override func viewDidLoad()
{
super.viewDidLoad()
mess = "123"
self.infoLabel.text = mess //label not getting updated
print(mess) //printing the required output in console
}