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

Lecture 5 View Controller Lifecycle

UITextView

UITextView

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

viewDidLoad

  • 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.

viewWillAppear

  • 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.

viewWillDisappear

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.

Autorotation

  • 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).

didReceiveMemoeryWarning

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

awakeFromNib

  • 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; }

Summary

NSNotification

Notifications

The “radio station”

NSNotificationCenter

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

[WWDC 2014] 402. Introduction to Swift

Key Principles

  • Safe
  • Modern
  • Power

Variables

Type Inference

Unicode Names

String

Building Complex Strings

String Mutability

Typed Collections

For-In: Ranges

For-In: Dictionaries

Modifying an Array

Optionals

Switch

  • Don’t need break !
  • Also not only int, but also objects!
  • siwtch must be exhaustive. default is necessary and important.

Default Parameter Values

Returning Values

Returning Multiple Values

Tuples

Decomposing a Tuples

Closures

Closures as Parameters

Classes

Computed Properties

Class Initialization

Overriding a Property

Property Observers

Structures in Swift

Structure or Class?

  • Class instances are passed by reference
  • Structures are passed by value

Mutating a Structure

Enumerations:Raw Values

Enumerations

Enumerations:Properties

Enumerations

Extensions

A Generic Stack Structure