0

When print(mess) is called from the viewDidLoad function, it prints what is expected in the console, but when it is assigned to a UITextField or UILabel, then the assigned value is not getting reflected in the UITextField or UILabel. why is this ?

Doing this:

var mess: String = ""
override func viewDidLoad()
{
    super.viewDidLoad()
    self.infoLabel.text = mess //label not getting updated

    print(mess) //printing the required output in console
}

But when I am doing this its working fine

override func viewDidLoad()
{
    super.viewDidLoad()
    let date = DATE()
    let calendar = Calendar.current
    let minutes = calendar.component(.minutes, from: date)
    temp.text = String(minutes)


}
san
  • 29
  • 6

2 Answers2

0

First assign value to mess variable then update UITextField value. At time of assign value to UITextField in viewDidLoad its have default value "".

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
    }
Needhi Roy
  • 160
  • 1
  • 6
0

Simple way you can reflate UILabel value using didSet

var mess:String = "" {
        didSet {
            setupLabel()
      }
 }

func setupLabel() {
     self.infoLabel.text = mass
}

Whenever mess value change it automatically infoLabel value change

iParesh
  • 2,338
  • 1
  • 18
  • 30
  • done this too but getting Fatal error: Unexpectedly found nil while unwrapping an Optional value – san Mar 08 '18 at 08:36