Slide 1

Slide 1 text

UIViewController Containers Matt Thomas @mthomas codecaffeine.com

Slide 2

Slide 2 text

What is a View Controller?

Slide 3

Slide 3 text

Model/View/Controller Controller Model View (happy fantasy world)

Slide 4

Slide 4 text

Model/View/Controller (reality) Controller Model View Model Controller Controller Controller Controller View View View Controller

Slide 5

Slide 5 text

Controller Controller What is a View Controller? Controller View View Controller A Controller coupled to a view

Slide 6

Slide 6 text

A View Controller • Manages Views (view property) • Manages Resources (didReceiveMemoryWarning:) • Responds to Events (viewDidRotate:) • Coordinate with Other Controllers View Controller Programming Guide for iOS

Slide 7

Slide 7 text

Types of View Controllers • Content View Controllers • Container View Controllers

Slide 8

Slide 8 text

Content View Controllers • Basic UIViewController or UITableViewController subclass • The view may be in an associated nib file or created programmatically in the viewController

Slide 9

Slide 9 text

Container View Controllers • Manages a Collection of View Controllers • Otherwise just an ordinary View Controller Controller View Controller View Controller View

Slide 10

Slide 10 text

Container View Controllers • UINavigationController • UITabBarController • UISplitViewController (iPad only) • UIPageViewController (iOS 5+) examples

Slide 11

Slide 11 text

Container View Controllers • Add each view controller’s view to my view • …and forward events… Easy, right? Controller View Controller View Controller View

Slide 12

Slide 12 text

Case Study • MGSplitViewController • Wonderful UISplitViewController replacement • https://github.com/mattgemmell/ MGSplitViewController/

Slide 13

Slide 13 text

Case Study - (void)willAnimateFirstHalfOfRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration { ! [self.masterViewController willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; ! [self.detailViewController willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; } A lot of code is needed to forward events:

Slide 14

Slide 14 text

Events you must implement • viewWillAppear: • viewDidAppear: • viewWillDisappear: • viewDidDisappear: • willRotateToInterfaceOrientation:duration: • didRotateFromInterfaceOrientation: • willAnimateRotationToInterfaceOrientation:duration: • willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: • didAnimateFirstHalfOfRotationToInterfaceOrientation: • willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration: • And don’t forget to call SUPER implementations! (even if you don’t use them in your class)

Slide 15

Slide 15 text

iOS 5 to the rescue? • A View Controller can now be added as a child of another View Controller • The events will automatically forward from parent to child Easy, right?

Slide 16

Slide 16 text

New UIViewController API • @property childViewControllers • addChildViewController: • removeFromParentViewController • willMoveToParentViewController: • didMoveToParentViewController: • automaticallyForwardAppearanceAndRotationMethodsToChildView Controllers • transitionFromViewController:toViewController:duration:opti ons:animations:completion:

Slide 17

Slide 17 text

“A Tale of Two* Hierarchies” *Three if you consider each UIView has a CALayer

Slide 18

Slide 18 text

Hierarchy From “Implementing UIViewController Containment” WWDC 2011

Slide 19

Slide 19 text

Hierarchy From “Implementing UIViewController Containment” WWDC 2011

Slide 20

Slide 20 text

Creating your own Container View Controller

Slide 21

Slide 21 text

Why? • iPad has lots of space • Encapsulate Content View Controller

Slide 22

Slide 22 text

Half Split View Controller Demo Left ViewController Right ViewController Top ViewController Bottom ViewController

Slide 23

Slide 23 text

Tri-Split View Controller Demo Left VC Main View Controller Right VC

Slide 24

Slide 24 text

Heck, Combine Them! Left VC Main View Controller Right VC Top ViewController Bottom ViewController

Slide 25

Slide 25 text

What did I just create? The apex of UI design :-/

Slide 26

Slide 26 text

Rethinking Container View Controllers (a rant) • “iPad has lots of space” is not a good reason • Easy to go overboard with UI • Best UI isn’t noticeable (iBooks) • Best UI is immersive (Garage Band) • Minimize “Computer Administrative Debris”

Slide 27

Slide 27 text

Rethinking Container View Controllers • Container View Controllers are about relationships • Parent-Child (UINavigationController) • Sibling (UITabBarController) • And interactions • Turn Pages (UIPageViewController)

Slide 28

Slide 28 text

Card Stack Controller Demo https://github.com/codecaffeine/CardStackController

Slide 29

Slide 29 text

Card Stack Controller Demo • Relationship • Each child view controller is atomic • Interaction • Focus on a view controller • See all view controllers

Slide 30

Slide 30 text

Implementing container View Controllers • Window must set rootViewController • Requirement for all apps iOS 4+ • rootViewController will be displayed fullscreen • Add children to View Controller hierarchy

Slide 31

Slide 31 text

Implementing container View Controllers • Must call didMoveToParentViewController: after addChildViewController: is called and view is added • Must call willMoveToParentViewController: before calling the removeFromParentViewController after view is removed

Slide 32

Slide 32 text

Implementing container View Controllers • transitionFromViewController: toViewController: duration: options: animations: completion: • Great for transitioning between two child View Controllers • Will automatically add/remove views to view hierarchy

Slide 33

Slide 33 text

Questions?