Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 328
webmagnets Avatar asked Apr 12 '15 13:04

webmagnets


People also ask

Is viewDidLoad only called once?

viewDidLoad() is one of the initialization methods that is called on the initial view controller. viewDidLoad() is called before anything is shown to the user - and it is called only once.

Does viewWillAppear get called before viewDidLoad?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.

What is the difference between viewDidLoad and viewWillAppear?

viewDidLoad ONLY gets called when the view is constructed - so for example after a view controller initFromNibNamed call when the view is accessed. viewWillAppear is called anytime your view controller was not in view but comes into view - so when your view controller is pushed, viewWillAppear is called.


1 Answers

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.

like image 74
nburk Avatar answered Nov 15 '22 05:11

nburk