the life and times of uiviewcontroller

33
The Life and Times of UIViewController Brandon Alexander Wednesday, September 18, 13

Upload: whilethis

Post on 10-May-2015

746 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: The Life and Times of UIViewController

The Life and Times of UIViewController

Brandon Alexander

Wednesday, September 18, 13

Page 2: The Life and Times of UIViewController

Who am IiOS Developer, Author, Speaker

Email: [email protected]

GTalk: [email protected]

Twitter/ADN: @balexander

Wednesday, September 18, 13

Page 3: The Life and Times of UIViewController

Design Pattern

A general reusable solution to a commonly occurring problem within a given context in software design

Wednesday, September 18, 13

Page 4: The Life and Times of UIViewController

MVC

• Model

• View

• Controller

Wednesday, September 18, 13

Page 5: The Life and Times of UIViewController

Controller

• Mediates between view and model

• Subclasses UIViewController

Wednesday, September 18, 13

Page 6: The Life and Times of UIViewController

Agenda

Wednesday, September 18, 13

Page 7: The Life and Times of UIViewController

Agenda

• UIViewController Lifecycle

Wednesday, September 18, 13

Page 8: The Life and Times of UIViewController

Agenda

• UIViewController Lifecycle

• Presenting other view controllers

Wednesday, September 18, 13

Page 9: The Life and Times of UIViewController

Agenda

• UIViewController Lifecycle

• Presenting other view controllers

• View Controller Containment

Wednesday, September 18, 13

Page 10: The Life and Times of UIViewController

Agenda

• UIViewController Lifecycle

• Presenting other view controllers

• View Controller Containment

• [REDACTED]

Wednesday, September 18, 13

Page 11: The Life and Times of UIViewController

Lifecycle

• Creation

• Interaction

• Destruction

Wednesday, September 18, 13

Page 12: The Life and Times of UIViewController

Creation

• NIB (or XIB)

• Storyboard

• In Code

Wednesday, September 18, 13

Page 13: The Life and Times of UIViewController

NIB

- (id) initWithNibName:(NSString *)n bundle:(NSBundle *)b

Wednesday, September 18, 13

Page 14: The Life and Times of UIViewController

Storyboard

- (instancetype) initWithCoder:(NSCoder *)c

Wednesday, September 18, 13

Page 15: The Life and Times of UIViewController

Code

- (instancetype) init- (instancetype) initWith...

- (void) loadView

Wednesday, September 18, 13

Page 16: The Life and Times of UIViewController

Creation Tips

• Wait for viewDidLoad for some startup items

• Don’t reference vc.view before viewDidLoad is called

• Use -[UIVC isViewLoaded]

• Use UIGestureRecognizer when possible

Wednesday, September 18, 13

Page 17: The Life and Times of UIViewController

Interaction

• Respond to user actions

• Delegate/Data Source Methods

• Respond to rotation events

• Respond to application notifications

• Navigate to other view controllers

Wednesday, September 18, 13

Page 18: The Life and Times of UIViewController

Important Methods- (void) viewWillAppear:(BOOL)animated- (void) viewDidAppear:(BOOL)animated

- (void) viewWillDisappear:(BOOL)animated- (void) viewDidDisappear:(BOOL)animated

- (void) viewWillLayoutSubviews- (void) viewDidLayoutSubviews

- (void) didReceiveMemoryWarning

Wednesday, September 18, 13

Page 19: The Life and Times of UIViewController

Rotation Support

• Info.plist shows all supported orientations

• Implement proper methods to support each orientation in each view controller

Wednesday, September 18, 13

Page 20: The Life and Times of UIViewController

Interaction Tips

• Split Data Source methods out to helper objects

• Keep the focus of the class in mind

Wednesday, September 18, 13

Page 21: The Life and Times of UIViewController

Destruction

• Implement dealloc when necessary

• Many existing UIKit classes aren’t ARCified

• Set delegate/dataSource properties to nil

Wednesday, September 18, 13

Page 22: The Life and Times of UIViewController

Presenting View Controllers

! //Create VC! UIViewController *vc = [UIViewController new];!! //Configure! vc.modalPresentationStyle = UIModalPresentationFormSheet;! vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;!! //Present! [self presentViewController:vc animated:YES completion:nil];

Wednesday, September 18, 13

Page 23: The Life and Times of UIViewController

Dismissing Presented View Controllers

• Multiple Approaches

• Delegation

• Post to the responder chain

• Don’t have a view controller dismiss itself

Wednesday, September 18, 13

Page 24: The Life and Times of UIViewController

View Controller Containment

• Keeps View Controller hierarchy the same as the view hierarchy

• Helps solve the Massive View Controller anti-pattern

Wednesday, September 18, 13

Page 25: The Life and Times of UIViewController

Adding a Child VC

- (void) displayContentController: (UIViewController*) content {! [self addChildViewController:content];! content.view.frame = [self frameForContentController];! [self.view addSubview:self.currentClientView];! [content didMoveToParentViewController:self];}

Wednesday, September 18, 13

Page 26: The Life and Times of UIViewController

Removing a child VC

- (void) hideContentController: (UIViewController*) content {! [content willMoveToParentViewController:nil];! [content.view removeFromSuperview];! [content removeFromParentViewController];}

Wednesday, September 18, 13

Page 27: The Life and Times of UIViewController

Other Containment Methods

- (BOOL) shouldAutomaticallyForwardAppearanceMethods- (BOOL) shouldAutomaticallyForwardRotationMethods

Wednesday, September 18, 13

Page 28: The Life and Times of UIViewController

iOS 7

• Custom View Controller Transitions

• Different layout paradigms

• -automaticallyAdjustsScrollViewInsets

• UIKit Dynamics

Wednesday, September 18, 13

Page 29: The Life and Times of UIViewController

Architecture

• Avoid Massive View Controllers

• Centralize networking code

• Controllers don’t have to be view controllers

Wednesday, September 18, 13

Page 30: The Life and Times of UIViewController

Other Tricks

• Use nil-target actions in views

• Lazy load in container view controllers

• Don’t bother cleaning up in will(Dis)Appear

Wednesday, September 18, 13

Page 31: The Life and Times of UIViewController

Resources

• View Controller Programming Guide

• About Cocoa Auto Layout

• iOS 7 UI Transition Guide

Wednesday, September 18, 13

Page 32: The Life and Times of UIViewController

Questions

Wednesday, September 18, 13

Page 33: The Life and Times of UIViewController

Contact

Email: [email protected]

GTalk: [email protected]

Twitter/ADN: @balexander

Wednesday, September 18, 13