uchicago-mobi / 2015-Winter-Forum

8 stars 1 forks source link

viewDidLoad vs viewDidAppear? #96

Closed cheng-wu closed 9 years ago

cheng-wu commented 9 years ago

So in the tutorial video, I saw instructor put the initialized refresh code into viewDidAppear, but in my code, I put it in viewDidLoad since I usually think there are same thing.

Can someone tell me what exact difference between viewDidLoad and viewDidAppear? in what situation, which one is preferred?

tabinks commented 9 years ago

Read this post about the view life cycle: http://chrisrisner.com/31-Days-of-iOS--Day-24%E2%80%93The-View-Life-Cycle

This is the order that UIViewController methods are called when being presented onscreen:

2012-12-27 12:27:35.942 DayTwentyFour[20082:c07] initWithCoder
2012-12-27 12:27:35.947 DayTwentyFour[20082:c07] loadView
2012-12-27 12:27:35.949 DayTwentyFour[20082:c07] viewDidLoad
2012-12-27 12:27:35.949 DayTwentyFour[20082:c07] viewWillAppear
2012-12-27 12:27:35.952 DayTwentyFour[20082:c07] viewWillLayoutSubviews
2012-12-27 12:27:35.952 DayTwentyFour[20082:c07] viewDidLayoutSubviews
2012-12-27 12:27:35.956 DayTwentyFour[20082:c07] viewDidAppear
JRam13 commented 9 years ago
2012-12-27 12:27:35.942 DayTwentyFour[20082:c07] initWithCoder
2012-12-27 12:27:35.947 DayTwentyFour[20082:c07] loadView
2012-12-27 12:27:35.949 DayTwentyFour[20082:c07] viewDidLoad

Will be only called once for the lifecycle of the viewcontroller. The others may be called multiple times i.e. viewDidAppear will be called every time you switch to the corresponding tab (good place to update your data).

tabinks commented 9 years ago

Good point. Also, remember that viewDidAppear is called each time the view appears when you switching tabs. Depending on the implementation, you may or may not want to load data here. If you expect the data to change frequently (e.g. a twitter stream, you may want to), if it doesn't change frequently (e.g. a RSS feed), you may not need to update here.

cheng-wu commented 9 years ago

Thank you! Get the idea!!!