当从print(mess)函数调用viewDidLoad时,它将打印控制台中期望的内容,但是当将其分配给UITextFieldUILabel时,分配的值不会反映在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
    }

08-17 05:43