본문 바로가기
모바일개발(Mobile Dev)/IOS개발(ObjectC)

difference between viewWillAppear viewDidAppear

by 테크한스 2015. 12. 27.
반응형

written by http://stackoverflow.com/questions/5630649/what-is-the-difference-between-viewwillappear-and-viewdidappear



What is the difference between -[UIViewController viewWillAppear:] and -[UIViewController viewDidAppear:]?

shareimprove this question
1 
thanks BoltClock, but please give me example of both if possible.. – PJR Apr 12 '11 at 10:19
3 
@BoltClock it would be nice if that were true. I'm guessing the 15 people who upvoted read the method name but never actually measured it ... Came here from Google because that is NOT the difference between them – Adam Oct 10 '12 at 16:55
   
Specifically: parentView.viewDidAppear is called A LONG TIME before Apple actually displays parentView ... Apple first (atomically) paints all subviews ... and if you have a lot of subviews, or complex ones, then "viewDidAppear" can be called tens or hundreds of milliseconds too soon :(. – Adam Oct 10 '12 at 16:57
up vote203down voteaccepted

In general, this is what I do:

1) ViewDidLoad - Whenever I'm adding controls to a view that should appear together with the view, right away, I put it in the ViewDidLoad method. Basically this method is called whenever the view was loaded into memory. So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without those forms.

2) ViewWillAppear: I use ViewWillAppear usually just to update the data on the form. So, for the example above, I would use this to actually load the data from my domain into the form. Creation of UIViews is fairly expensive, and you should avoid as much as possible doing that on the ViewWillAppear method, becuase when this gets called, it means that the iPhone is already ready to show the UIView to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).

3) ViewDidAppear: Finally, I use the ViewDidAppear to start off new threads to things that would take a long time to execute, like for example doing a webservice call to get extra data for the form above.The good thing is that because the view already exists and is being displayed to the user, you can show a nice "Waiting" message to the user while you get the data.

shareimprove this answer
2 
Sorry, but what do you mean by "load the data from my domain into the form" in viewWillAppear? You mean downloading through network? But you also suggest download stuff in viewDidAppear? – Philip007Dec 3 '12 at 9:28 
1 
@Philip007 I think Stack is referring to this type of domain: en.wikipedia.org/wiki/Domain-specific_modeling. The data is loaded from your models or similar. – dentarg Dec 20 '12 at 14:45
2 
This answer should be in the docs. It was really helpful in clarifying the difference between the three methods. Thank you! – GangstaGraham Oct 30 '13 at 19:34
   
@GangstaGraham Welcome my friend... – Chetan Bhalara Nov 7 '13 at 5:55
1 
+1 I was having a little bit of confusion understanding the difference between these three, but you just cleared it up more than perfectly @ChetanBhalara – Chisx Jan 12 '14 at 19:54

viewDidLoad ===>>> Put your initialization code here. Don't put dynamic data that might change during the view lifecycle. So, if you are pulling data from core data you don't want to do it here if this could change during the life of the view. For example: say you have a tab controller. You switch from tab1 to tab2 and change something on the model in tab2. If you come back to tab1 and your model code was done in viewDidLoad this would not be updated (assuming you're not using KVO or NSFetchedResultsController, etc.).

viewWillAppear ===>>> This is called every time the view is about to appear, whether or not the view is already in memory. Put your dynamic code here, such as model logic.

viewDidAppear ===>>> Put expensive operations here that you only want to do if you're sure the view is onscreen, such as network calls.

Notice: if your app is backgrounded and returns to the foreground you need to handle this using NSNotificationCenter. I wrote the code out for that in the comments below. You might think viewWillAppear/viewDidAppear will fire. Put a break point there and test it. It doesn't fire. So, if something has changed for your app while it was in the background you'll need to update that using notifications.

shareimprove this answer
1 
Does ViewWill or ViewDid get run each time you un-minimize the application? – Jeef Apr 2 '14 at 20:30
1 
@Jeef This is an excellent question. Neither gets run unless the app is killed by the system or the user while in the background. What you have to do to get notified when the app in un-minimized is you have to use NSNotificationCenter and addObserver for the name UIApplicationWillEnterForegroundNotification. The selector should be applicationWillEnterForeground: it has a NSNotification paramater. Put your code in that method for reloading data, etc. What you can do is create a reload method that you call form this method and also viewDidAppear if they need to be the same. – smileBot Apr 2 '14 at 22:36
   
@Jeef something like this: - (void)viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil]; } - (void) applicationWillEnterForeground: (NSNotification *)notif {// respond here with whatever } – smileBot Apr 2 '14 at 22:39 

The viewWillAppear method is called before loading the actual view.

The viewDidAppear method is called when the view is already loaded, and you want to show something.

shareimprove this answer
6 
Incorrect word loaded. Correct one is appeared – purrrminator May 8 '14 at 8:31

viewwillappear will call before loading the view so that you can do certain task before loading that view and viewdidappear will call after loading the view so the post task will done in that method

shareimprove this answer

Difference between "will" and "did"...As the name suggests the viewWillAppear is called before the view is about to appear and viewDidAppear is called when view did appear.

shareimprove this answer
   
look at the accepted answer bro, which contains 70+ upvotes. :) – PJR Apr 15 '13 at 11:53
   
I know, I am newbie here.. Building repo.. – Mahesh Apr 16 '13 at 4:16
   
okay..Great and yes WELCOME :) – PJR Apr 16 '13 at 4:43

The former happens before the view appears and the latter happens afterwards.

shareimprove this answer

viewWillAppear:
■ Called before the view is added to the windows’ view hierarchy
■ Called before [vc.view layoutSubviews] (if necessary)
viewDidAppear:
■ Called after the view is added to the view hierarchy
■ Called after [vc.view layoutSubviews] (if necessary)

shareimprove this answer

As the name suggests the viewWillAppear is called before the view is about to appear and viewDidAppear is called when view did appear.

shareimprove this answer

To sum up:

-viewWillAppear -> update data (reload data from a table view)

-viewDidAppear -> expensive operations (API call with a nice progress hud!)


반응형