I am pretty fresh in ios dev world. just wondering should I always do it in viewDidLoad method? Also is it a good idea to always release开发者_高级运维 it in viewDidUnload? Thank you.
Usually either in awakeFromNib, viewDidLoad, or viewWillAppear. Beginners usually start by putting everything in viewDidLoad which is fine for a simple view but there are reasons to use the other methods.
awakeFromNib is called when the nib is unpacked. Here you can add any additional views or set attributes that aren't available in IB. If you're not using IB then this doesn't get called.
viewDidLoad is called when the "view" is loaded (obvio). This should be used for adding view objects (typically subclasses of UIView) or updating them with current data. Any objects allocated here should be released in viewDidUnload (which may be called when memory is low). This gets called when a view is put into a window (triggered by UIViewController's view getter method) and isn't currently loaded (views get unloaded on a memory warning if they are not currently visible).
viewWillAppear is called anytime the view is going to appear in the window. This is the best place to update the data in your view with current data. This always gets called when you return from another modal view or pop another view controller or switch back to this app from the background or come back from a phone call, etc. Make sure you data is updated here or you may be showing stale data. Be sure to call [super viewWillAppear] at some point in this method.
See also these SO answers: here and here
精彩评论