• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Creating Container View Controllers
 

Creating Container View Controllers

on

  • 35,173 views

Presentation given at 2012 360iDev Conference.

Presentation given at 2012 360iDev Conference.

Statistics

Views

Total Views
35,173
Views on SlideShare
33,451
Embed Views
1,722

Actions

Likes
28
Downloads
246
Comments
3

5 Embeds 1,722

http://www.bobmccune.com 1700
https://twitter.com 18
http://www.linkedin.com 2
http://translate.googleusercontent.com 1
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

13 of 3 previous next Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Thanks for that! You saved my life!
    Are you sure you want to
    Your message goes here
    Processing…
  • Love it!
    Are you sure you want to
    Your message goes here
    Processing…
  • Thanks Bob. I had written my own hack to accomplish this, but after upgrading to Xcode 4.5 and iOS 6 it broke. Thanks for providing the correct way to accomplish this!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Creating Container View Controllers Creating Container View Controllers Presentation Transcript

    • CreatingContainer View Controllers http://bobmccune.com
    • About...Bob McCune ‣ MN Developer and Instructor ‣ Owner of TapHarmonic, LLC. ‣ Founded Minnesota CocoaHeads in 2008
    • AgendaWhat will I learn?‣ View Controller Overview‣ Custom Containers Before iOS 5‣ iOS 5’s View Controller Containment API‣ Custom Container Demo
    • View Controller Overview
    • What is a View Controller?View Controller Overview‣ Focal point of most iOS app development‣ Key Responsibilities: ‣ Defines the application workflow ‣ Manages a view hierarchy ‣ Programmatically ‣ NIB and/or Storyboard‣ Plays the MVC “Controller” role...
    • Understanding MVCView Controller: The “C” in MVC Model View Update State Controller User Actions
    • Understanding MVCView Controller: The “C” in MVC Model View State Changed Controller Update UI
    • MVC BenefitsCore iOS Design Pattern ‣ Clean separation of concerns ‣ Simplifies development ‣ Provides for greater reusability ‣ Improves quality ‣ Allows us to standardize the behavior and responsibilities at each tier
    • UIViewController LifecycleStandardized Behavior‣ Loading Callbacks - (void)viewDidLoad; - (void)viewDidUnload;‣ Appearance Callbacks - (void)viewWillAppear: - (void)viewDidAppear: - (void)viewWillDisappear: - (void)viewDidDisappear:‣ Rotation Callbacks - (void)willRotateToInterfaceOrientation: - (void)willAnimateRotationToInterfaceOrientation: - (void)didRotateFromInterfaceOrientation:
    • View Controller TypesContainer vs ContentContainer Controllers‣ Manages a hierarchy of child view controllers UITabBarController UINavigationController UISplitViewControllerContent Controllers‣ Manage the individual screens within an app‣ Can be used in multiple presentation contexts‣ Manages a “screenful of content”
    • Screenful of ContentSeems reasonable...
    • Screenful of ContentStill reasonable?
    • UISplitViewControllerWhat’s a screenful?
    • Why Create Custom Containers?One Screen, Multiple Controllers ‣ Aesthetics ‣ Create a custom application flow
    •  Pre  -­  iOS  5Custom ContainersThe heartbreaking true story
    • Custom ContainersFaulty Assumptions Static  Logo
    • Custom ContainersFaulty Assumptions Static  Logo
    • Custom ContainersFaulty Assumptions Static  Logo
    • Custom ContainersFaulty Assumptions can’t! No you Static  Logo
    • What’s the problem?Custom Container Fail‣ Appearance Callbacks - (void)viewWillAppear: - (void)viewDidAppear: - (void)viewWillDisappear: - (void)viewDidDisappear:‣ Rotation Callbacks - (void)willRotateToInterfaceOrientation: - (void)willAnimateRotationToInterfaceOrientation: - (void)didRotateFromInterfaceOrientation:‣ Broken View Controller Hierarchy
    • How do you fix it?Ugly OptionsCreate a MonstrosityControllerNot practicalCreate non-UIViewController controllersNot scalableCreate container and forward callbacksIncomplete and ugly
    • View Controller Containment
    • Object HierarchiesView vs View Controller View Hierarchy Window Root View NavBar Segmented Tab Bar
    • Object HierarchiesView vs View Controller View Controller Hierarchy UITabBarController UINavigationController ContentViewController
    • View Controller ContainmentSimple, but subtleAdding and removing child controllers- (void)addChildViewController:(UIViewController *)controller;- (void)removeFromParentViewController;Accessing the children@property(nonatomic,readonly) NSArray *children;Child View Controller Callbacks- (void)willMoveToParentViewController:(UIViewController *)parent;- (void)didMoveToParentViewController:(UIViewController *)parent;
    • Containment API UsageAdding a Child View Controller [self addChildViewController:controller]; [self.view addSubview:controller.view]; [controller didMoveToParentViewController:self]; view ParentViewControllerwillMove view ChildViewController
    • Containment API UsageAdding a Child View Controller [self addChildViewController:controller]; [self.view addSubview:controller.view]; [controller didMoveToParentViewController:self]; view ParentViewController view ChildViewController
    • Containment API UsageAdding a Child View Controller [self addChildViewController:controller]; [self.view addSubview:controller.view]; [controller didMoveToParentViewController:self]; view ParentViewControllerdidMove view ChildViewController
    • Containment API UsageRemoving a Child View Controller [controller willMoveToParentViewController:nil]; [controller.view removeFromSuperview]; [controller removeFromParentViewController]; view ParentViewControllerwillMove view ChildViewController
    • Containment API UsageRemoving a Child View Controller [controller willMoveToParentViewController:nil]; [controller.view removeFromSuperview]; [controller removeFromParentViewController]; view ParentViewController view ChildViewController
    • Containment API UsageRemoving a Child View Controller [controller willMoveToParentViewController:nil]; [controller.view removeFromSuperview]; [controller removeFromParentViewController]; view ParentViewControllerdidMove view ChildViewController
    • View Controller TransitionsSimplifying Transitions- (void)transitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))block;‣ Convenience method for view controller transitions‣ Optional, but simplifies and normalizes transitioning
    • Cloning UINavigationControllerpushViewController:animated:- (void)pushViewController:(UIViewController *)toViewController animated:(BOOL)animated { UIViewController *fromViewController = [self.stack topObject]; toViewController.view.frame = CGRectMake(width, 0.f, width, height); [self addChildViewController:toViewController]; NSTimeInterval duration = animated ? 0.3f : 0.f; [self transitionFromViewController:fromViewController toViewController:toViewController duration:duration options:UIViewAnimationCurveEaseInOut animations:^{ CGRect frame = CGRectMake(-width, 0.f, width, height); fromViewController.view.frame = frame; } completion:^(BOOL complete) { [toViewController didMoveToParentViewController:self]; [self.stack pushObject:toViewController]; }];}
    • Cloning UINavigationControllerpopViewControllerAnimated:- (UIViewController *)popViewControllerAnimated:(BOOL)animated { UIViewController *fromViewController = [self.stack popObject]; UIViewController *toViewController = [self.stack topObject]; [fromViewController willMoveToParentViewController:nil]; NSTimeInterval duration = animated ? 0.3f : 0.0f; [self transitionFromViewController:fromViewController toViewController:toViewController duration:duration options:UIViewAnimationOptionCurveEaseInOut animations:^{ CGRect frame = CGRectMake(width, 0.f, width, height); fromViewController.view.frame = frame; } completion:^(BOOL complete) { [fromViewController removeFromParentViewController]; }]; return fromViewController;}
    • Disabling Auto ForwardingFine Tuning Containment- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers{ return NO;}• Control timing of appearance and rotation callbacks• Useful override in complex containment scenarios
    • Avoiding Common Mistakes
    • Common MistakesSimple API, Common Problems ‣ Outside Callers ‣ Disobedient Children ‣ Meddling Parents
    • Outside Callers Drive-by Adoption ModalViewController RootViewController ChildViewController- (IBAction)showModalView:(id)sender { ModalViewController *modalController = [ModalViewController controller]; [self presentViewController:modalController animated:YES completion:nil]; ChildViewController *childController = [ChildViewController controller]; [modalController addChildViewController:childController]; [modalController addSubview:childController.view];}
    • Disobedient ChildrenParents make the rules CustomContainerController OtherViewController ChildViewControllerCustomContainerController- (void)showChildViewController:(UIViewController *)controller { [self addChildViewController:controller]; controller.view.frame = CGRectMake(0, 0, 320, 480); [controller didMoveToParentViewController:self];} [self.view addSubview:controller.view];ChildViewController- (void)didMoveToParentViewController:(UIViewController *)parent { self.view.frame = CGRectMake(0, 260, 320, 220); [parent.view addSubview:self.view];}
    • Disobedient ChildrenParents make the rules CustomContainerController OtherViewController ChildViewControllerCustomContainerController- (void)showChildViewController:(UIViewController *)controller { [self addChildViewController:controller]; controller.view.frame = CGRectMake(0, 0, 320, 480); [controller didMoveToParentViewController:self];} [self.view addSubview:controller.view];ChildViewController- (void)didMoveToParentViewController:(UIViewController *)parent { self.view.frame = CGRectMake(0, 260, 320, 220); [parent.view addSubview:self.view];}
    • Disobedient ChildrenParents make the rules CustomContainerController OtherViewController ChildViewControllerCustomContainerController- (void)showChildViewController:(UIViewController *)controller { [self addChildViewController:controller]; controller.view.frame = CGRectMake(0, 260, 320, 220); [self.view addSubview:controller.view]; [controller didMoveToParentViewController:self];}
    • Meddling ParentsLet children be children ParentViewController ChildViewController
    • Meddling ParentsLet children be children ParentViewController ChildViewControllerParentViewController- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration { self.childViewController.button1.frame = // button 1 frame for orientation; self.childViewController.button2.frame = // button 2 frame for orientation; self.childViewController.button3.frame = // button 3 frame for orientation;}
    • Meddling ParentsLet children be children ParentViewController ChildViewControllerChildViewController- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration { self.button1.frame = // button 1 frame for orientation; self.button2.frame = // button 2 frame for orientation; self.button3.frame = // button 3 frame for orientation;}
    • Demo
    • SummaryView Controller Containment FTW! ‣ Simple, but subtle API. Easy to make mistakes. ‣ Need to understand UIViewController internals ‣ Small, but important, enhancements in iOS 6
    • ResourcesPresentation Materialshttp://www.slideshare.net/bobmccune/https://github.com/tapharmonic/WWDC 2011: Implementing View Controller Containmenthttps://developer.apple.com/videos/wwdc/2011/?id=102WWDC 2012: The Evolution of View Controllers on iOShttps://developer.apple.com/videos/wwdc/2012/?id=236 BobMcCune.com @bobmccune