Swift: Reload a View Controller Swift: Reload a View Controller ios ios

Swift: Reload a View Controller


Whatever code you are writing in viewDidLoad, Add that in viewWillappear(). This will solve your problem.


You shouldn't call viewDidLoad method manually,Instead if you want to reload any data or any UI, you can use this:

override func viewDidLoad() {    super.viewDidLoad();    let myButton = UIButton()    // When user touch myButton, we're going to call loadData method    myButton.addTarget(self, action: #selector(self.loadData), forControlEvents: .TouchUpInside)    // Load the data    self.loadData();}func loadData() {    // code to load data from network, and refresh the interface    tableView.reloadData()}

Whenever you want to reload the data and refresh the interface, you can call self.loadData()


In Swift 4:

self.view.layoutIfNeeded()