What is the difference between -[UIViewController viewWillAppear:]
and -[UIViewController viewDidAppear:]
?
ベストアンサー1
In general, this is what I do:
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 these forms.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 ofUIViews
is fairly expensive, and you should avoid as much as possible doing that on theViewWillAppear
method because when this gets called, it means that the iPhone is already ready to show theUIView
to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).ViewDidAppear: Finally, I'm using
ViewDidAppear
to start off new threads to things that would take a long time to execute, like for example doing a web service 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.