How can I completely reload a view so that viewDidLoad gets run again?

不羁的心 提交于 2019-12-22 06:28:22

问题


I would like to reload my view so that viewDidLoad redoes all of its work. How can I do that in Swift?


回答1:


viewViewDidLoad and viewWillAppear are part of the view controller lifecycle and are called automatically by iOS after your UIViewController got instantiated and before the view appears on the screen. You can call them anywhere in your view controller class, but that's not at all good practice and I would never recommend you to do so!

If you have code that you want to be executed at multiple times during the lifetime of your view controller, put it into separate methods and call these from viewDidLoad and from the other parts in your code where you need them.

You can read more about the view controller life cycle in Apple's documentation.




回答2:


You could call ViewDidLoad manually but it is not recommended. The best thing is to put all your code inside viewDidLoad in another methods (like loadData). And call this method again when you need it.

But it depends why you need it. Often ViewWillAppear or ViewDidAppear are a better choice.



来源:https://stackoverflow.com/questions/29590053/how-can-i-completely-reload-a-view-so-that-viewdidload-gets-run-again

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!