Search code examples
iosuiviewcontrollerviewwillappear

May I use viewWillAppear instead of viewDidLoad for most of my initialization and setup?


I'm not good in communicating between controllers, so i need to setup and check a lot from NSUserDefaults, may it cause any problems?


Solution

  • It depends on what you intend to do after the form start-to-finish of loading the view.

    You don't want to do too much in viewWillAppear (Called when the view is ready to be shown) as it could impact performance; typically you want to do things like refresh a table, or update the text on a label etc. The viewDidLoad method is called once the view is loaded and it is common to add things like buttons, labels etc, anything that you want to appear on the view. If you have anything tasks that may take long to execute, it is better to do them in the viewDidAppear as the view has already been loaded; it is good practice to perform these methods on a separate thread, or at least provide the user with some sort of activity indicator until the work is done.