Road To iOS Series 2

September 1, 2014

View Controller Lifecycle (UIViewController_Class)

Overview

  1. awakeFromNib, nib here means storyboard
  2. outlets get set
  3. viewDidLoad
  4. viewWillLayoutSubviews: and viewDidLayoutSubviews:
  5. viewWillAppear: and viewDidAppear: etc. (both repeatedly)
  6. 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
- (void)setup{}; //dosomething which can’t wait until viewDidLoad
- (void)awakeFromNib { [self setup]; }

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!
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *outlineButton;
@end

-(void)viewDidLoad
{
	// always call super for letting superclass init.
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

	// because we are not textview, we have to convert title to mutablestring
    // then set the title
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:self.outlineButton.currentTitle];
    [title setAttributes:@{ NSStrokeWidthAttributeName : @5,
                            NSStrokeColorAttributeName : self.outlineButton.tintColor}
                   range:NSMakeRange(0, title.length)];

    [self.outlineButton setAttributedTitle:title forState:UIControlStateNormal];
}

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.
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated]; // call super in all the viewWill/Did... methods
// let’s be nice to the user and remember the scroll position they were at ...
[self rememberScrollPosition]; // we’ll have to implement this
// do some other clean up now that we’ve been removed from the screen
[self saveDataToPermanentStore]; // maybe do in did instead?
// but be careful not to do anything time-consuming here, or app will be sluggish
// maybe even kick off a thread to do what needs doing here
}

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

 UIViewController Lifecycle

image source


Reference

  1. Stanford iOS 7 development
  2. Mac Developer Library
  3. UIViewController_Class Reference
  4. ViewController Programming
  5. Stackoverflow