0

As per my understanding, viewDidLoad() is called when the view is created and used for things that need to happen once whereas viewWillAppear() is used for tasks that require to repeat every time a VC comes on screen.

What is the difference in tasks doing them once and repeating whenever VC comes on a screen?

Currently, I have a View with a bunch of UILabels and UITextFields. Technically, I need to only create and set the text only once but each time you visit that View it will need to be done. Would that logic make sense more in a viewWillAppear() in comparison to a viewDidLoad().

My reasoning to put them in viewDidLoad() was that they only need to happen once. Then again, you can argue that each time you're on that specific View it will need to happen each time. Unsure if my understanding is correct or not...or if I'm overthinking

1
  • Viewdidload getting called after the UIView is prepared in memory. If the view is not removed from the memory, it won’t get called. Commented Dec 8, 2018 at 23:49

3 Answers 3

0

It's somewhat difficult to answer your question without having more information about what exactly it is you want to achieve, but it might be helpful to keep in mind this diagram about the view controller life cycle:

View Controller Life Cycle

If you place a UILabel or UITextField on a view in viewDidLoad, they will continue to be there every time viewWillAppear() is called, and thus you do not need to instantiate those labels or fields again in viewWillAppear().

Sign up to request clarification or add additional context in comments.

Comments

0

You miss a point that is whenever you create a new instance of the vc it's viewDidLoad is called once , that means if you dismiss that vc or pop it ( if it's inside a navigation controller ) then it completely deallocates ( if no strong references exists ) , that's a story with 1 instance , if you presented another instance it'll go the same , But

For the lifeTime of 1 instance viewDidLoad is called once , and viewWillAppear/viewDidAppear are called whenever you dismiss a vc that's presented by that instance or pop that child from the stack

Inject the setup code inside viewDidLoad , and any content that you want to refresh set it in viewWillAppear/viewDidAppear for ex suppose you have a table lists some items and you add the item in a modal vc , then you go place tableView.reloadData() in viewWillAppear , but for adding the tableView to the view , you make that inside viewDidLoad

Comments

0

Here is how they could be used in a simple game workflow:

  • instantiate a UILabel (a game score, for example) in ViewDidLoad of a main menu UIView

  • load the main menu

  • present a game scene, play game, etc...

  • get a new high score, the main menus ViewDidAppear updates the UILabel with the new score, and when the game scene is dismissed the UILabel will reflect the new score.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.