Share
Embed
UIViewController Containers
My phillycocoa.org presentation on UIViewController Containers from 2/9/2012
Matt Thomas
February 11, 2012
Transcript
33 slides
-
Model/View/Controller (reality) Controller Model View Model Controller Controller Controller Controller
View View View Controller -
Controller Controller What is a View Controller? Controller View View
Controller A Controller coupled to a view -
A View Controller • Manages Views (view property) • Manages
Resources (didReceiveMemoryWarning:) • Responds to Events (viewDidRotate:) • Coordinate with Other Controllers View Controller Programming Guide for iOS -
Types of View Controllers • Content View Controllers • Container
View Controllers -
Content View Controllers • Basic UIViewController or UITableViewController subclass •
The view may be in an associated nib file or created programmatically in the viewController -
Container View Controllers • Manages a Collection of View Controllers
• Otherwise just an ordinary View Controller Controller View Controller View Controller View -
Container View Controllers • UINavigationController • UITabBarController • UISplitViewController (iPad
only) • UIPageViewController (iOS 5+) examples -
Container View Controllers • Add each view controller’s view to
my view • …and forward events… Easy, right? Controller View Controller View Controller View -
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: -
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) -
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? -
New UIViewController API • @property childViewControllers • addChildViewController: • removeFromParentViewController
• willMoveToParentViewController: • didMoveToParentViewController: • automaticallyForwardAppearanceAndRotationMethodsToChildView Controllers • transitionFromViewController:toViewController:duration:opti ons:animations:completion: -
“A Tale of Two* Hierarchies” *Three if you consider each
UIView has a CALayer -
Why? • iPad has lots of space • Encapsulate Content
View Controller -
Half Split View Controller Demo Left ViewController Right ViewController Top
ViewController Bottom ViewController -
Heck, Combine Them! Left VC Main View Controller Right VC
Top ViewController Bottom ViewController -
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” -
Rethinking Container View Controllers • Container View Controllers are about
relationships • Parent-Child (UINavigationController) • Sibling (UITabBarController) • And interactions • Turn Pages (UIPageViewController) -
Card Stack Controller Demo • Relationship • Each child view
controller is atomic • Interaction • Focus on a view controller • See all view controllers -
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 -
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 -
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