当从 viewDidLoad
函数调用 print(mess)
时,它会打印控制台中预期的内容,但是当它被分配给 UITextField
或 UILabel
,则分配的值不会反射(reflect)在 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)
}
最佳答案
首先为困惑变量赋值,然后更新 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
}
关于ios - 如何在 iOS 中将字符串变量分配给标签或文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49167170/