Search code examples
iosswiftviewdidload

How to reload view after data changes in Swift


I have a view that contains labels, and have a button to change the values of the labels. Instead of changing the values one by one in the button, how can I reload the whole view to update the labels.

@IBOutlet weak var one: UILabel!
    @IBOutlet weak var two: UILabel!
    @IBOutlet weak var three: UILabel!
....
    @IBOutlet weak var updateLabels: UIButton!{
        //doing something to change the value of the labels
        //then wanna reload the whole view
        viewDidLoad()
    }

I had called the viewDidLoad() method, but didn't work.


Solution

  • You should never call viewDidLoad yourself. It's a framework function that the OS calls, as an indication that your views are ready to be setup.

    It would serve better if you separated your function

    func updateLabels() {
            one.text = "one"
            two.text = "two"
            three.text = "three"
    }
    

    and now you can call the updateLabels function when you want.