SlideShare a Scribd company logo
Creating
Container View
   Controllers

        http://bobmccune.com
About...
Bob McCune
 ‣ MN Developer and Instructor
 ‣ Owner of TapHarmonic, LLC.
 ‣ Founded Minnesota CocoaHeads in 2008
Agenda
What 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 MVC
View Controller: The “C” in MVC



   Model                                  View




    Update State   Controller     User Actions
Understanding MVC
View Controller: The “C” in MVC



   Model                                View




  State Changed    Controller     Update UI
MVC Benefits
Core 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 Lifecycle
Standardized 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 Types
Container vs Content
Container Controllers
‣ Manages a hierarchy of child view controllers

 UITabBarController
 UINavigationController
 UISplitViewController

Content Controllers
‣ Manage the individual screens within an app

‣ Can be used in multiple presentation contexts

‣ Manages a “screenful of content”
Screenful of Content
Seems reasonable...
Screenful of Content
Still reasonable?
UISplitViewController
What’s a screenful?
Why Create Custom Containers?
One Screen, Multiple Controllers
 ‣ Aesthetics
 ‣ Create a custom application flow
 Pre  -­  iOS  5
Custom Containers
The heartbreaking true story
Custom Containers
Faulty Assumptions


                     Static  Logo
Custom Containers
Faulty Assumptions


                     Static  Logo
Custom Containers
Faulty Assumptions


                     Static  Logo
Custom Containers
Faulty 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 Options
Create a MonstrosityController
Not practical
Create non-UIViewController controllers
Not scalable
Create container and forward callbacks
Incomplete and ugly
View Controller Containment
Object Hierarchies
View vs View Controller

    View Hierarchy

           Window



          Root View



               NavBar



                 Segmented



     Tab Bar
Object Hierarchies
View vs View Controller
                          View Controller Hierarchy

                               UITabBarController




                                   UINavigationController




                                        ContentViewController
View Controller Containment
Simple, but subtle
Adding 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 Usage
Adding a Child View Controller
   [self addChildViewController:controller];
   [self.view addSubview:controller.view];
   [controller didMoveToParentViewController:self];

                                         view
           ParentViewController


willMove


                                  view
           ChildViewController
Containment API Usage
Adding a Child View Controller
 [self addChildViewController:controller];
 [self.view addSubview:controller.view];
 [controller didMoveToParentViewController:self];

                           view
    ParentViewController




                           view
    ChildViewController
Containment API Usage
Adding a Child View Controller
   [self addChildViewController:controller];
   [self.view addSubview:controller.view];
   [controller didMoveToParentViewController:self];

                                 view
          ParentViewController


didMove


                                 view
          ChildViewController
Containment API Usage
Removing a Child View Controller
   [controller willMoveToParentViewController:nil];
   [controller.view removeFromSuperview];
   [controller removeFromParentViewController];

                                  view
           ParentViewController


willMove


                                  view
           ChildViewController
Containment API Usage
Removing a Child View Controller
 [controller willMoveToParentViewController:nil];
 [controller.view removeFromSuperview];
 [controller removeFromParentViewController];

                                  view
    ParentViewController




                           view
    ChildViewController
Containment API Usage
Removing a Child View Controller
   [controller willMoveToParentViewController:nil];
   [controller.view removeFromSuperview];
   [controller removeFromParentViewController];

                                        view
          ParentViewController


didMove


                                 view
          ChildViewController
View Controller Transitions
Simplifying 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 UINavigationController
pushViewController: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 UINavigationController
popViewControllerAnimated:
- (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 Forwarding
Fine Tuning Containment
- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers
{
    return NO;
}

• Control timing of appearance and rotation callbacks
• Useful override in complex containment scenarios
Avoiding
 Common Mistakes
Common Mistakes
Simple 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 Children
Parents make the rules

                            CustomContainerController
    OtherViewController
                               ChildViewController




CustomContainerController
-    (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 Children
Parents make the rules

                            CustomContainerController
    OtherViewController
                               ChildViewController




CustomContainerController
-    (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 Children
Parents make the rules

                         CustomContainerController
 OtherViewController
                            ChildViewController




CustomContainerController
- (void)showChildViewController:(UIViewController *)controller {
	 [self addChildViewController:controller];
  controller.view.frame = CGRectMake(0, 260, 320, 220);
  [self.view addSubview:controller.view];
	 [controller didMoveToParentViewController:self];
}
Meddling Parents
Let children be children


     ParentViewController




     ChildViewController
Meddling Parents
Let children be children


       ParentViewController




        ChildViewController




ParentViewController
- (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 Parents
Let children be children


       ParentViewController




        ChildViewController




ChildViewController
- (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
Summary
View Controller Containment FTW!
 ‣ Simple, but subtle API. Easy to make mistakes.
 ‣ Need to understand UIViewController internals
 ‣ Small, but important, enhancements in iOS 6
Resources
Presentation Materials
http://www.slideshare.net/bobmccune/
https://github.com/tapharmonic/

WWDC 2011: Implementing View Controller Containment
https://developer.apple.com/videos/wwdc/2011/?id=102

WWDC 2012: The Evolution of View Controllers on iOS
https://developer.apple.com/videos/wwdc/2012/?id=236




    BobMcCune.com                                 @bobmccune

More Related Content

What's hot

Ionic adventures - Hybrid Mobile App Development rocks
Ionic adventures - Hybrid Mobile App Development rocksIonic adventures - Hybrid Mobile App Development rocks
Ionic adventures - Hybrid Mobile App Development rocks
Juarez Filho
 
UI5con 2018: News from Control Development
UI5con 2018: News from Control DevelopmentUI5con 2018: News from Control Development
UI5con 2018: News from Control Development
Andreas Kunz
 
Atomic Designは「マルチ」で真価を発揮する
Atomic Designは「マルチ」で真価を発揮するAtomic Designは「マルチ」で真価を発揮する
Atomic Designは「マルチ」で真価を発揮する
Yukiya Nakagawa
 
Android Wear: A Developer's Perspective
Android Wear: A Developer's PerspectiveAndroid Wear: A Developer's Perspective
Android Wear: A Developer's Perspective
Vin Lim
 
Meetup angular http client
Meetup angular http clientMeetup angular http client
Meetup angular http client
Gaurav Madaan
 
Angular
AngularAngular
Predictable Web Apps with Angular and Redux
Predictable Web Apps with Angular and ReduxPredictable Web Apps with Angular and Redux
Predictable Web Apps with Angular and Redux
FITC
 
PhotoFlipCardView
PhotoFlipCardViewPhotoFlipCardView
PhotoFlipCardView
Katsumi Kishikawa
 
AngularJS for Beginners
AngularJS for BeginnersAngularJS for Beginners
AngularJS for Beginners
Edureka!
 
Create twitter-ios-app
Create twitter-ios-appCreate twitter-ios-app
Create twitter-ios-app
Tsuneo Yoshioka
 
Mobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhoneMobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhoneMohammad Shaker
 
Mobile Software Engineering Crash Course - C04 Android Cont.
Mobile Software Engineering Crash Course - C04 Android Cont.Mobile Software Engineering Crash Course - C04 Android Cont.
Mobile Software Engineering Crash Course - C04 Android Cont.Mohammad Shaker
 
JavaScript : One To Many
JavaScript : One To ManyJavaScript : One To Many
JavaScript : One To Many
Jamel Eddine Mejri
 
Adding User Management to Node.js
Adding User Management to Node.jsAdding User Management to Node.js
Adding User Management to Node.js
Dev_Events
 
WebView Development Pitfalls
WebView Development PitfallsWebView Development Pitfalls
WebView Development Pitfalls
tonypai
 
I/O Rewind 215: What's new in Android
I/O Rewind 215: What's new in AndroidI/O Rewind 215: What's new in Android
I/O Rewind 215: What's new in Android
Sittiphol Phanvilai
 
Support Design Library
Support Design LibrarySupport Design Library
Support Design Library
Taeho Kim
 
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
🎤 Hanno Embregts 🎸
 
Ionic2, les développeurs web à l'assaut du mobile, BDX I/O le 21/10/2016
Ionic2, les développeurs web à l'assaut du mobile, BDX I/O le 21/10/2016Ionic2, les développeurs web à l'assaut du mobile, BDX I/O le 21/10/2016
Ionic2, les développeurs web à l'assaut du mobile, BDX I/O le 21/10/2016
Loïc Knuchel
 
Multi Window in Android N
Multi Window in Android NMulti Window in Android N
Multi Window in Android N
Taeho Kim
 

What's hot (20)

Ionic adventures - Hybrid Mobile App Development rocks
Ionic adventures - Hybrid Mobile App Development rocksIonic adventures - Hybrid Mobile App Development rocks
Ionic adventures - Hybrid Mobile App Development rocks
 
UI5con 2018: News from Control Development
UI5con 2018: News from Control DevelopmentUI5con 2018: News from Control Development
UI5con 2018: News from Control Development
 
Atomic Designは「マルチ」で真価を発揮する
Atomic Designは「マルチ」で真価を発揮するAtomic Designは「マルチ」で真価を発揮する
Atomic Designは「マルチ」で真価を発揮する
 
Android Wear: A Developer's Perspective
Android Wear: A Developer's PerspectiveAndroid Wear: A Developer's Perspective
Android Wear: A Developer's Perspective
 
Meetup angular http client
Meetup angular http clientMeetup angular http client
Meetup angular http client
 
Angular
AngularAngular
Angular
 
Predictable Web Apps with Angular and Redux
Predictable Web Apps with Angular and ReduxPredictable Web Apps with Angular and Redux
Predictable Web Apps with Angular and Redux
 
PhotoFlipCardView
PhotoFlipCardViewPhotoFlipCardView
PhotoFlipCardView
 
AngularJS for Beginners
AngularJS for BeginnersAngularJS for Beginners
AngularJS for Beginners
 
Create twitter-ios-app
Create twitter-ios-appCreate twitter-ios-app
Create twitter-ios-app
 
Mobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhoneMobile Software Engineering Crash Course - C06 WindowsPhone
Mobile Software Engineering Crash Course - C06 WindowsPhone
 
Mobile Software Engineering Crash Course - C04 Android Cont.
Mobile Software Engineering Crash Course - C04 Android Cont.Mobile Software Engineering Crash Course - C04 Android Cont.
Mobile Software Engineering Crash Course - C04 Android Cont.
 
JavaScript : One To Many
JavaScript : One To ManyJavaScript : One To Many
JavaScript : One To Many
 
Adding User Management to Node.js
Adding User Management to Node.jsAdding User Management to Node.js
Adding User Management to Node.js
 
WebView Development Pitfalls
WebView Development PitfallsWebView Development Pitfalls
WebView Development Pitfalls
 
I/O Rewind 215: What's new in Android
I/O Rewind 215: What's new in AndroidI/O Rewind 215: What's new in Android
I/O Rewind 215: What's new in Android
 
Support Design Library
Support Design LibrarySupport Design Library
Support Design Library
 
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
Building a Spring Boot Application - Ask the Audience! (from JVMCon 2018)
 
Ionic2, les développeurs web à l'assaut du mobile, BDX I/O le 21/10/2016
Ionic2, les développeurs web à l'assaut du mobile, BDX I/O le 21/10/2016Ionic2, les développeurs web à l'assaut du mobile, BDX I/O le 21/10/2016
Ionic2, les développeurs web à l'assaut du mobile, BDX I/O le 21/10/2016
 
Multi Window in Android N
Multi Window in Android NMulti Window in Android N
Multi Window in Android N
 

Viewers also liked

Core Animation
Core AnimationCore Animation
Core Animation
Bob McCune
 
Composing and Editing Media with AV Foundation
Composing and Editing Media with AV FoundationComposing and Editing Media with AV Foundation
Composing and Editing Media with AV Foundation
Bob McCune
 
Objective-C for Java Developers
Objective-C for Java DevelopersObjective-C for Java Developers
Objective-C for Java Developers
Bob McCune
 
Drawing with Quartz on iOS
Drawing with Quartz on iOSDrawing with Quartz on iOS
Drawing with Quartz on iOS
Bob McCune
 
Master Video with AV Foundation
Master Video with AV FoundationMaster Video with AV Foundation
Master Video with AV Foundation
Bob McCune
 
Mastering Media with AV Foundation
Mastering Media with AV FoundationMastering Media with AV Foundation
Mastering Media with AV Foundation
Chris Adamson
 
Quartz 2D with Swift 3
Quartz 2D with Swift 3Quartz 2D with Swift 3
Quartz 2D with Swift 3
Bob McCune
 
Starting Core Animation
Starting Core AnimationStarting Core Animation
Starting Core Animation
John Wilker
 
Building Modern Audio Apps with AVAudioEngine
Building Modern Audio Apps with AVAudioEngineBuilding Modern Audio Apps with AVAudioEngine
Building Modern Audio Apps with AVAudioEngine
Bob McCune
 
Animation in iOS
Animation in iOSAnimation in iOS
Animation in iOS
Alexis Goldstein
 
20 iOS developer interview questions
20 iOS developer interview questions20 iOS developer interview questions
20 iOS developer interview questions
Arc & Codementor
 
iOS Developer Interview Questions
iOS Developer Interview QuestionsiOS Developer Interview Questions
iOS Developer Interview Questions
Clark Davidson
 
Core Graphics & Core Animation
Core Graphics & Core AnimationCore Graphics & Core Animation
Core Graphics & Core AnimationAndreas Blick
 
Graphics Libraries
Graphics LibrariesGraphics Libraries
Graphics Libraries
Prachi Mishra
 
Forward Swift 2017: Media Frameworks and Swift: This Is Fine
Forward Swift 2017: Media Frameworks and Swift: This Is FineForward Swift 2017: Media Frameworks and Swift: This Is Fine
Forward Swift 2017: Media Frameworks and Swift: This Is Fine
Chris Adamson
 
try! Swift - Advanced Graphics with Core Animation
try! Swift - Advanced Graphics with Core Animationtry! Swift - Advanced Graphics with Core Animation
try! Swift - Advanced Graphics with Core Animation
Tim Oliver
 
Designing better user interfaces
Designing better user interfacesDesigning better user interfaces
Designing better user interfaces
Johan Ronsse
 
iOS Coding Best Practices
iOS Coding Best PracticesiOS Coding Best Practices
iOS Coding Best Practices
Jean-Luc David
 
iOS design: a case study
iOS design: a case studyiOS design: a case study
iOS design: a case study
Johan Ronsse
 
iOS Scroll Performance
iOS Scroll PerformanceiOS Scroll Performance
iOS Scroll Performance
Kyle Sherman
 

Viewers also liked (20)

Core Animation
Core AnimationCore Animation
Core Animation
 
Composing and Editing Media with AV Foundation
Composing and Editing Media with AV FoundationComposing and Editing Media with AV Foundation
Composing and Editing Media with AV Foundation
 
Objective-C for Java Developers
Objective-C for Java DevelopersObjective-C for Java Developers
Objective-C for Java Developers
 
Drawing with Quartz on iOS
Drawing with Quartz on iOSDrawing with Quartz on iOS
Drawing with Quartz on iOS
 
Master Video with AV Foundation
Master Video with AV FoundationMaster Video with AV Foundation
Master Video with AV Foundation
 
Mastering Media with AV Foundation
Mastering Media with AV FoundationMastering Media with AV Foundation
Mastering Media with AV Foundation
 
Quartz 2D with Swift 3
Quartz 2D with Swift 3Quartz 2D with Swift 3
Quartz 2D with Swift 3
 
Starting Core Animation
Starting Core AnimationStarting Core Animation
Starting Core Animation
 
Building Modern Audio Apps with AVAudioEngine
Building Modern Audio Apps with AVAudioEngineBuilding Modern Audio Apps with AVAudioEngine
Building Modern Audio Apps with AVAudioEngine
 
Animation in iOS
Animation in iOSAnimation in iOS
Animation in iOS
 
20 iOS developer interview questions
20 iOS developer interview questions20 iOS developer interview questions
20 iOS developer interview questions
 
iOS Developer Interview Questions
iOS Developer Interview QuestionsiOS Developer Interview Questions
iOS Developer Interview Questions
 
Core Graphics & Core Animation
Core Graphics & Core AnimationCore Graphics & Core Animation
Core Graphics & Core Animation
 
Graphics Libraries
Graphics LibrariesGraphics Libraries
Graphics Libraries
 
Forward Swift 2017: Media Frameworks and Swift: This Is Fine
Forward Swift 2017: Media Frameworks and Swift: This Is FineForward Swift 2017: Media Frameworks and Swift: This Is Fine
Forward Swift 2017: Media Frameworks and Swift: This Is Fine
 
try! Swift - Advanced Graphics with Core Animation
try! Swift - Advanced Graphics with Core Animationtry! Swift - Advanced Graphics with Core Animation
try! Swift - Advanced Graphics with Core Animation
 
Designing better user interfaces
Designing better user interfacesDesigning better user interfaces
Designing better user interfaces
 
iOS Coding Best Practices
iOS Coding Best PracticesiOS Coding Best Practices
iOS Coding Best Practices
 
iOS design: a case study
iOS design: a case studyiOS design: a case study
iOS design: a case study
 
iOS Scroll Performance
iOS Scroll PerformanceiOS Scroll Performance
iOS Scroll Performance
 

Similar to Creating Container View Controllers

Using view controllers wisely
Using view controllers wiselyUsing view controllers wisely
Using view controllers wisely
defagos
 
Intro to UIKit • Made by Many
Intro to UIKit • Made by ManyIntro to UIKit • Made by Many
Intro to UIKit • Made by Many
kenatmxm
 
Session 15 - Working with Image, Scroll, Collection, Picker, and Web View
Session 15  - Working with Image, Scroll, Collection, Picker, and Web ViewSession 15  - Working with Image, Scroll, Collection, Picker, and Web View
Session 15 - Working with Image, Scroll, Collection, Picker, and Web View
Vu Tran Lam
 
IOS APPs Revision
IOS APPs RevisionIOS APPs Revision
IOS APPs Revision
Muhammad Amin
 
iPhoneOS3.1でのカメラAPIについて
iPhoneOS3.1でのカメラAPIについてiPhoneOS3.1でのカメラAPIについて
iPhoneOS3.1でのカメラAPIについてKyosuke Takayama
 
Leaving Interface Builder Behind
Leaving Interface Builder BehindLeaving Interface Builder Behind
Leaving Interface Builder Behind
John Wilker
 
Practical Protocol-Oriented-Programming
Practical Protocol-Oriented-ProgrammingPractical Protocol-Oriented-Programming
Practical Protocol-Oriented-Programming
Natasha Murashev
 
Practialpop 160510130818
Practialpop 160510130818Practialpop 160510130818
Practialpop 160510130818
Shahzain Saeed
 
MCE^3 - Natasha Murashev - Practical Protocol-Oriented Programming in Swift
MCE^3 - Natasha Murashev - Practical Protocol-Oriented Programming in SwiftMCE^3 - Natasha Murashev - Practical Protocol-Oriented Programming in Swift
MCE^3 - Natasha Murashev - Practical Protocol-Oriented Programming in Swift
PROIDEA
 
App coordinators in iOS
App coordinators in iOSApp coordinators in iOS
App coordinators in iOS
Uptech
 
Use case driven architecture
Use case driven architectureUse case driven architecture
Use case driven architecture
Bohdan Orlov
 
CocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIViewCocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads France
 
Using a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsUsing a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS apps
allanh0526
 
아이폰강의(5) pdf
아이폰강의(5) pdf아이폰강의(5) pdf
아이폰강의(5) pdfsunwooindia
 
Swf2 ui
Swf2 uiSwf2 ui
View controllers en iOS
View controllers en iOSView controllers en iOS
View controllers en iOS
Mariano Carrizo
 
From mvc to viper
From mvc to viperFrom mvc to viper
From mvc to viper
Krzysztof Profic
 

Similar to Creating Container View Controllers (20)

I os 11
I os 11I os 11
I os 11
 
Using view controllers wisely
Using view controllers wiselyUsing view controllers wisely
Using view controllers wisely
 
004
004004
004
 
Intro to UIKit • Made by Many
Intro to UIKit • Made by ManyIntro to UIKit • Made by Many
Intro to UIKit • Made by Many
 
Session 15 - Working with Image, Scroll, Collection, Picker, and Web View
Session 15  - Working with Image, Scroll, Collection, Picker, and Web ViewSession 15  - Working with Image, Scroll, Collection, Picker, and Web View
Session 15 - Working with Image, Scroll, Collection, Picker, and Web View
 
IOS APPs Revision
IOS APPs RevisionIOS APPs Revision
IOS APPs Revision
 
iPhoneOS3.1でのカメラAPIについて
iPhoneOS3.1でのカメラAPIについてiPhoneOS3.1でのカメラAPIについて
iPhoneOS3.1でのカメラAPIについて
 
Leaving Interface Builder Behind
Leaving Interface Builder BehindLeaving Interface Builder Behind
Leaving Interface Builder Behind
 
Practical Protocol-Oriented-Programming
Practical Protocol-Oriented-ProgrammingPractical Protocol-Oriented-Programming
Practical Protocol-Oriented-Programming
 
Practialpop 160510130818
Practialpop 160510130818Practialpop 160510130818
Practialpop 160510130818
 
MCE^3 - Natasha Murashev - Practical Protocol-Oriented Programming in Swift
MCE^3 - Natasha Murashev - Practical Protocol-Oriented Programming in SwiftMCE^3 - Natasha Murashev - Practical Protocol-Oriented Programming in Swift
MCE^3 - Natasha Murashev - Practical Protocol-Oriented Programming in Swift
 
занятие6
занятие6занятие6
занятие6
 
App coordinators in iOS
App coordinators in iOSApp coordinators in iOS
App coordinators in iOS
 
Use case driven architecture
Use case driven architectureUse case driven architecture
Use case driven architecture
 
CocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIViewCocoaHeads Toulouse - Guillaume Cerquant - UIView
CocoaHeads Toulouse - Guillaume Cerquant - UIView
 
Using a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS appsUsing a model view-view model architecture for iOS apps
Using a model view-view model architecture for iOS apps
 
아이폰강의(5) pdf
아이폰강의(5) pdf아이폰강의(5) pdf
아이폰강의(5) pdf
 
Swf2 ui
Swf2 uiSwf2 ui
Swf2 ui
 
View controllers en iOS
View controllers en iOSView controllers en iOS
View controllers en iOS
 
From mvc to viper
From mvc to viperFrom mvc to viper
From mvc to viper
 

Recently uploaded

Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 

Recently uploaded (20)

Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 

Creating Container View Controllers

  • 1. Creating Container View Controllers http://bobmccune.com
  • 2. About... Bob McCune ‣ MN Developer and Instructor ‣ Owner of TapHarmonic, LLC. ‣ Founded Minnesota CocoaHeads in 2008
  • 3. Agenda What will I learn? ‣ View Controller Overview ‣ Custom Containers Before iOS 5 ‣ iOS 5’s View Controller Containment API ‣ Custom Container Demo
  • 4. View Controller Overview
  • 5. 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...
  • 6. Understanding MVC View Controller: The “C” in MVC Model View Update State Controller User Actions
  • 7. Understanding MVC View Controller: The “C” in MVC Model View State Changed Controller Update UI
  • 8. MVC Benefits Core 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
  • 9. UIViewController Lifecycle Standardized 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:
  • 10. View Controller Types Container vs Content Container Controllers ‣ Manages a hierarchy of child view controllers UITabBarController UINavigationController UISplitViewController Content Controllers ‣ Manage the individual screens within an app ‣ Can be used in multiple presentation contexts ‣ Manages a “screenful of content”
  • 11. Screenful of Content Seems reasonable...
  • 14. Why Create Custom Containers? One Screen, Multiple Controllers ‣ Aesthetics ‣ Create a custom application flow
  • 15.  Pre  -­  iOS  5 Custom Containers The heartbreaking true story
  • 19. Custom Containers Faulty Assumptions can’t! No you Static  Logo
  • 20. 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
  • 21. How do you fix it? Ugly Options Create a MonstrosityController Not practical Create non-UIViewController controllers Not scalable Create container and forward callbacks Incomplete and ugly
  • 23. Object Hierarchies View vs View Controller View Hierarchy Window Root View NavBar Segmented Tab Bar
  • 24. Object Hierarchies View vs View Controller View Controller Hierarchy UITabBarController UINavigationController ContentViewController
  • 25. View Controller Containment Simple, but subtle Adding 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;
  • 26. Containment API Usage Adding a Child View Controller [self addChildViewController:controller]; [self.view addSubview:controller.view]; [controller didMoveToParentViewController:self]; view ParentViewController willMove view ChildViewController
  • 27. Containment API Usage Adding a Child View Controller [self addChildViewController:controller]; [self.view addSubview:controller.view]; [controller didMoveToParentViewController:self]; view ParentViewController view ChildViewController
  • 28. Containment API Usage Adding a Child View Controller [self addChildViewController:controller]; [self.view addSubview:controller.view]; [controller didMoveToParentViewController:self]; view ParentViewController didMove view ChildViewController
  • 29. Containment API Usage Removing a Child View Controller [controller willMoveToParentViewController:nil]; [controller.view removeFromSuperview]; [controller removeFromParentViewController]; view ParentViewController willMove view ChildViewController
  • 30. Containment API Usage Removing a Child View Controller [controller willMoveToParentViewController:nil]; [controller.view removeFromSuperview]; [controller removeFromParentViewController]; view ParentViewController view ChildViewController
  • 31. Containment API Usage Removing a Child View Controller [controller willMoveToParentViewController:nil]; [controller.view removeFromSuperview]; [controller removeFromParentViewController]; view ParentViewController didMove view ChildViewController
  • 32. View Controller Transitions Simplifying 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
  • 33. Cloning UINavigationController pushViewController: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]; }]; }
  • 34. Cloning UINavigationController popViewControllerAnimated: - (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; }
  • 35. Disabling Auto Forwarding Fine Tuning Containment - (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers { return NO; } • Control timing of appearance and rotation callbacks • Useful override in complex containment scenarios
  • 37. Common Mistakes Simple API, Common Problems ‣ Outside Callers ‣ Disobedient Children ‣ Meddling Parents
  • 38. 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]; }
  • 39. Disobedient Children Parents make the rules CustomContainerController OtherViewController ChildViewController CustomContainerController - (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]; }
  • 40. Disobedient Children Parents make the rules CustomContainerController OtherViewController ChildViewController CustomContainerController - (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]; }
  • 41. Disobedient Children Parents make the rules CustomContainerController OtherViewController ChildViewController CustomContainerController - (void)showChildViewController:(UIViewController *)controller { [self addChildViewController:controller]; controller.view.frame = CGRectMake(0, 260, 320, 220); [self.view addSubview:controller.view]; [controller didMoveToParentViewController:self]; }
  • 42. Meddling Parents Let children be children ParentViewController ChildViewController
  • 43. Meddling Parents Let children be children ParentViewController ChildViewController ParentViewController - (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; }
  • 44. Meddling Parents Let children be children ParentViewController ChildViewController ChildViewController - (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; }
  • 45. Demo
  • 46. Summary View Controller Containment FTW! ‣ Simple, but subtle API. Easy to make mistakes. ‣ Need to understand UIViewController internals ‣ Small, but important, enhancements in iOS 6
  • 47. Resources Presentation Materials http://www.slideshare.net/bobmccune/ https://github.com/tapharmonic/ WWDC 2011: Implementing View Controller Containment https://developer.apple.com/videos/wwdc/2011/?id=102 WWDC 2012: The Evolution of View Controllers on iOS https://developer.apple.com/videos/wwdc/2012/?id=236 BobMcCune.com @bobmccune