[iOS 7 Stanford 2013 Fall] Lecture 5 View Controller Lifecycle

Lecture 5 View Controller Lifecycle



UITextView: Like UILabel, but multi-line, selectable/editable, scrollable, etc

How to set text view’s NSMutableAttributedString?

  • Obtain the NSMutableAttributedString representing the text in the UITextView using

  • NSTextStorage is a subclass of NSMutableAttributedString

Setting the font

  • Set font: @property (nonatomic, strong) UIFont *font;

Advanced layout in UITextView with TextKit

  • This property defines “where text can be” in the UITextView

  • This object will read characters from textStorage and lays down glyphs into textContainer

View Controller Lifecycle


  • After instantiation and outlet-setting, viewDidLoad is called.
  • This an exceptionally good place to put a lot of setup code.
  • However, geometry of view (its bounds) is not set yet!
  • At this point, you can’t be sure you’re on an iPhone 5-sized screen or an iPad or ???. (WTF?)
  • Do not initialize things that are geometry-dependent here.


  • View will only get “loaded” once, but it might appear and disappear a lot.
  • Do something here if things you display are changing while your MVC is off-screen.
  • View’s geometry is set here, but there are other (better) places to react to geometry.


Geometry changed?

  • Most of the time this will be automatically handled with Autolayout.

  • Called any time a view’s frame changed and its subviews were thus re-layed out.

  • Set the frames of subviews here or set other geometry-affecting properties.

  • Between “will” and “did”, autolayout will happen.


  • The view controller returns YES from shouldAutorotate
  • The view controller returns the new orientation in supportedInterfaceOrientations
  • The application allows rotation to that orientation (defined in Info.plist file).


  • In low-memory, this method is called.
  • Anything “big”, such as images and sounds that can be recreated should probably be released.


  • This method is sent to all objects that come out of a storyboard (including Controllers).
  • Happens before outlets are set!
  • Anything that would go in your Controller’s init method would have to go in awakeFromNib too, because init methods are not called on objects that come from a storyboard.

    • (void)setup { };
    • (void)awakeFromNib { [self setup]; }
    • (instancetype)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle { self = [super initWithNibName:name bundle:bundle]; [self setup]; return self; }




The “radio station”


Get the default “notification center” via [NSNotificationCenter defaultCenter]

Receive the notification

Tune out when done listening

  • A good place to do so is viewWillDisappear:
  • Or in a method called dealloc

Leave a Reply

Your email address will not be published. Required fields are marked *