View Controller Lifecycle (UIViewController_Class)
Overview
awakeFromNib
, nib here means storyboard
- outlets get set
viewDidLoad
viewWillLayoutSubviews:
and viewDidLayoutSubviews:
viewWillAppear:
and viewDidAppear:
etc. (both repeatedly)
didReceiveMemoryWarning
awakeFromNib
- (void)awakeFromNib
- Sent to all objects that come out of a storyboard (including your Controller)
- Happens before outlets are set! (i.e. before the MVC is “loaded”)
- After an object receives an awakeFromNib message, it is guaranteed to have all its outlet instance variables set
- Anything that would go in your Controller’s init method would have to go in awakeFromNib
viewDidLoad
- Only get called once
- After instantiation and outlet-setting, viewDidLoad is called
- Before the actual screen shows up
- Geometry of your view (its bounds) is not set yet!
- A fantistic place for init. Better than your controller init. for your outlet now is set!
viewWillLayoutSubviews: and viewDidLayoutSubviews:
- Called any time a view’s frame changed and its subviews were thus re-layed out. e.g. autorotation
- Put Geometry codes here
viewWillAppear: and viewDidAppear: (both repeatedly)
- Repeatedly: view will only get “loaded”(viewDidLoad) once, but it might appear and disappear a lot.
- (void)viewWillAppear:(BOOL)animated;
a place to do something if things you display are changing while your MVC is off-screen.
- (void)viewWillDisappear:(BOOL)animated
a place to put “remember what’s going on” and cleanup code.
didReceiveMemoryWarning
- iOS gets its right to kill your app if you are a memory eater…
- put clean up and dealloc codes here if necessary
- set
strong
pointer to nil
Picture of View Controller Lifecycle
image source
Reference
- Stanford iOS 7 development
- Mac Developer Library
- UIViewController_Class Reference
- ViewController Programming
- Stackoverflow