SlideShare a Scribd company logo
Using view controllers
        wisely
    Samuel Défago,         le studio
            October 2011
Who am I?
• iOS developer at          le studio
  (www.hortis.ch)
• Mail: defagos (at) gmail (dot) com
• Twitter: @defagos
• Blog: http://subjective-objective-
  c.blogspot.com/
• Open-source: https://github.com/defagos
Motivations

• VCs are fundamental iOS objects, ...
• ... are used and implemented every day...
• ... but are often misused and abused
• When correctly used, they lead to more
  reliable applications and superior code
Goals

• Learn how to organize VCs in applications
• Understand how to use containers


• Avoid common pitfalls and errors
Misused? Abused?
• Messy implementation
• Incorrect resource management
• Often only slightly superior to Xcode
  templates (i.e. slightly superior to void)
• Poor separation of concerns, big balls of
  mud
• Monolithic application workflow
yep,here

               Topics
                         Havealookinthecorner
                         toknowwhichtopicwe
                                   aretalkingabout


• Overview of view controllers  containers
• Using view controllers in an application
• Anatomy of containers
What is a view
         controller?
• is a controller: Bi-directional
  communication channel between view and
  model (MVC)
• has a view:View hierarchy management
  (lifecycle, orientation, low-memory
  conditions)
• Manages the content of one screen or of
  one part of a screen
• Subclass to create custom view controllers
What is a container?
• Is an object managing a set of view
  controllers
• In general used as-is, not meant to be
  subclassed
• Helps managing the view controller
  hierarchy, i.e. the application workflow...
• ... but only the workflow it implements
UINavigationController




• Navigation bar at the top
• Push from right / left animations only
UITabBarController



• Tabs at the bottom
• Must be application root VC or root in
  modal
Criticism
• Few built-in containers...
• ... i.e. few well-supported workflows
  (navigation, tabs, modal, split content)
• This is annoying on iPad (emphasis on
  creative interfaces, use of both hands), less
  on iPhone (skinned interfaces, one hand
  usually suffices)
An iPad shopping
        application
• Application to buy items from some online
  store on the iPad
• Three people involved:
 • Product owner: Requirements
 • UI / UX designer: Design  interaction
 • Programmer: Write the application
Product owner

• “I want the customer to be able to browse
  products and their descriptions”
• “I want the customer to be able to add
  items to its cart”
• “I want the customer to be able to access
  its cart and to submit an order”
UI / UX designer (1)
UI / UX designer (2)
Programmer:VCs
ShoppingViewController

                                                    Product-
    ProductList-
                                                 ViewController
    ViewController


ShoppingViewController                     ShoppingViewController


    CartItemList-         Stayin     BillingInformation-
    ViewController       carttab        ViewController
Programmer: Workflow
                           retains VC and
                          addSubview:
ShoppingViewController                          ProductListViewController
                                                                                   presentModalViewController:
            showShop:                                            showDetails:               animated:

            showCart:         retains VC and                                                         ProductViewController
                             addSubview:
            checkout:
                                                                                                                    close:
                                               CartItemListViewController
           goBackwards:
                                                                                                        dismissModalViewController
                                                                showDetails:                                    Animated:
                                                                                presentModalViewController:
                                                                 checkout:               animated:




                                               BillingInformationViewController
                           retains VC and
                          addSubview:
                                                                    submitOrder:

                                                                    goBackwards:


                                                                                                                     IBAction
Demo
Criticism

• No transparency when displaying product
  details
• ShoppingViewController manages almost
  all application workflow
• Seems to work, but does it REALLY work?
Analyzing the shopping
      application
• Use VCs logging all lifecycle and rotation
  events
• Navigate the application
• Rotate the device
• Simulate memory warnings
• Check the logs
Log archaeology (1)

• Click on Shop tab, then on Cart tab:


• Show product details
Log archaeology (2)
                           retains VC and
                          addSubview:
ShoppingViewController                          ProductListViewController
                                                                                   presentModalViewController:
            showShop:                                            showDetails:               animated:

            showCart:         retains VC and                                                         ProductViewController
                             addSubview:
            checkout:
                                                                                                                      close:
                                               CartItemListViewController
           goBackwards:
                                                                                                        dismissModalViewController
                                                                showDetails:                                    Animated:
                                                                                presentModalViewController:
                                                                 checkout:               animated:




                                               BillingInformationViewController
                           retains VC and
                          addSubview:
                                                                    submitOrder:
                                                                                                   View lifecycle and rotation OK
                                                                    goBackwards:


                                                                                                   View lifecycle and rotation KO
Conclusion
• Creating a view controllers hierarchy using
  addSubview:   leads to incorrect behavior
• View lifecycle and rotation events are not
  emitted automagically
• The root and modal view controllers have a
  proper behavior

      Always use containers to manage your
      view controller hierarchy
Custom containers

• Custom containers are needed for more
  creative workflows
• Using custom containers must be easy...
• ... but writing custom containers is hard
Elementary containers
• Writing containers is hard...
• ... so write few correct elementary
  containers...
• ... and combine them to create new
  workflows...
• ... or even new containers!
Placeholders
• Embed some other view controllers
• Display VCs simultaneously
• Can display other UI elements
                     VC1             VC1
  VC1
                                              VC3
                         VC2         VC2
 1-placeholder       2-placeholder    3-placeholder
Stack
• Manage a stack of view controllers
• Usually only display the top VC
• Fill the whole view, no other UI elements

                   VC4
                   VC1
                    VC1
                    VC1
Combinations (1)


     VC1
                = custom UITabBarController




1-placeholder
Combinations (2)

                   +          VC4
                              VC1
                               VC1
                               VC1

     VC1
                               stack




1-placeholder   = custom UINavigationController
Combinations (3)


VC1                   VC2




      2-placeholder



             = resizable UISplitViewController
Implementation

• CoconutKit (https://github.com/defagos/
  CoconutKit):
  • HLSPlaceholderViewController
  • HLSStackController
• Technical details: http://subjective-objective-
  c.blogspot.com/2011/08/writing-high-
  quality-view-controller.html
Demo
iShopping revisited
ShoppingViewController(PH)

                                     stack                  Product-
       ProductList-
                                                         ViewController
       ViewController


ShoppingViewController(PH)           ShoppingViewController(PH)


       CartItemList-                               BillingInformation-
                                     stack            ViewController
       ViewController
Buggy workflow
                           retains VC and
                          addSubview:
ShoppingViewController                          ProductListViewController
                                                                                   presentModalViewController:
            showShop:                                            showDetails:               animated:

            showCart:         retains VC and                                                         ProductViewController
                             addSubview:
            checkout:
                                                                                                                    close:
                                               CartItemListViewController
           goBackwards:
                                                                                                        dismissModalViewController
                                                                showDetails:                                    Animated:
                                                                                presentModalViewController:
                                                                 checkout:               animated:




                                               BillingInformationViewController
                           retains VC and
                          addSubview:
                                                                    submitOrder:

                                                                    goBackwards:
Fixed workflow
  ShoppingViewController
 (stack root  placeholder)                     ProductListViewController
                                                                                pushViewController:
               showShop:                                         showDetails:
                              setInsetViewController:
                  showCart:                                                                        ProductViewController

                                                             pushViewController:                                     close:

                                                                                                            popViewController:

                                                                 §
                                            CartItemListViewController
                                                    (stack root)
                                                            showDetails:

                                                             checkout:                 BillingInformationViewController
                                                                   pushViewController:
                                                                                                            submitOrder:

                                                                                                            goBackwards:

                                         StackController                                                popViewController:
StackController
Demo
Log archaeology

• Click on Cart tab when on Shop tab:


• Show product details
Summary

• Never ever display a view controller by
  adding its view as subview directly
• Design your view controller workflow, and
  manage it using view controller containers
• Use the view controller lifecycle correctly
  to insert your code where most
  appropriate
Thanks for your
   attention
Questions?

More Related Content

PDF
Ctools presentation
ODP
A Complete Tour of JSF 2
PDF
Java server faces
PPT
805 14-kevin
PDF
CoconutKit
PDF
Mobilizare civica prin mass-media - campania Salvati Padurile Virgine
PDF
iOS developer toolbox
PDF
32 Ways a Digital Marketing Consultant Can Help Grow Your Business
Ctools presentation
A Complete Tour of JSF 2
Java server faces
805 14-kevin
CoconutKit
Mobilizare civica prin mass-media - campania Salvati Padurile Virgine
iOS developer toolbox
32 Ways a Digital Marketing Consultant Can Help Grow Your Business

Similar to Using view controllers wisely (20)

PDF
아이폰강의(5) pdf
PDF
Creating Container View Controllers
KEY
занятие6
PPTX
Android and IOS UI Development (Android 5.0 and iOS 9.0)
PPTX
View controllers en iOS
PDF
iOS Transition Animations The proper way to do it.pdf
PDF
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
PDF
IOS APPs Revision
PPTX
Objective c design pattens-architetcure
PPTX
App Project Planning, by Apple
PDF
Refreshing Your App in iOS 7
PDF
07 Navigation Tab Bar Controllers
PDF
Ios 7 Programming Cookbook 2nd Edition Vandad Nahavandipoor
PDF
Formacion en movilidad: Conceptos de desarrollo en iOS (III)
KEY
storyboard時代のInterfaceBuilder
PDF
Session 15 - Working with Image, Scroll, Collection, Picker, and Web View
KEY
iOS View Coordinators
PPTX
iOS Development (Part 2)
PDF
Session 14 - Working with table view and search bar
PDF
Modular View Controller Hierarchies
아이폰강의(5) pdf
Creating Container View Controllers
занятие6
Android and IOS UI Development (Android 5.0 and iOS 9.0)
View controllers en iOS
iOS Transition Animations The proper way to do it.pdf
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 05)
IOS APPs Revision
Objective c design pattens-architetcure
App Project Planning, by Apple
Refreshing Your App in iOS 7
07 Navigation Tab Bar Controllers
Ios 7 Programming Cookbook 2nd Edition Vandad Nahavandipoor
Formacion en movilidad: Conceptos de desarrollo en iOS (III)
storyboard時代のInterfaceBuilder
Session 15 - Working with Image, Scroll, Collection, Picker, and Web View
iOS View Coordinators
iOS Development (Part 2)
Session 14 - Working with table view and search bar
Modular View Controller Hierarchies
Ad

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Cloud computing and distributed systems.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Machine Learning_overview_presentation.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
PPTX
A Presentation on Artificial Intelligence
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
cuic standard and advanced reporting.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Electronic commerce courselecture one. Pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Cloud computing and distributed systems.
The Rise and Fall of 3GPP – Time for a Sabbatical?
Assigned Numbers - 2025 - Bluetooth® Document
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation_ Review paper, used for researhc scholars
Machine Learning_overview_presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology
A Presentation on Artificial Intelligence
Programs and apps: productivity, graphics, security and other tools
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation theory and applications.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
cuic standard and advanced reporting.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Electronic commerce courselecture one. Pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Ad

Using view controllers wisely

  • 1. Using view controllers wisely Samuel Défago, le studio October 2011
  • 2. Who am I? • iOS developer at le studio (www.hortis.ch) • Mail: defagos (at) gmail (dot) com • Twitter: @defagos • Blog: http://subjective-objective- c.blogspot.com/ • Open-source: https://github.com/defagos
  • 3. Motivations • VCs are fundamental iOS objects, ... • ... are used and implemented every day... • ... but are often misused and abused • When correctly used, they lead to more reliable applications and superior code
  • 4. Goals • Learn how to organize VCs in applications • Understand how to use containers • Avoid common pitfalls and errors
  • 5. Misused? Abused? • Messy implementation • Incorrect resource management • Often only slightly superior to Xcode templates (i.e. slightly superior to void) • Poor separation of concerns, big balls of mud • Monolithic application workflow
  • 6. yep,here Topics Havealookinthecorner toknowwhichtopicwe aretalkingabout • Overview of view controllers containers • Using view controllers in an application • Anatomy of containers
  • 7. What is a view controller? • is a controller: Bi-directional communication channel between view and model (MVC) • has a view:View hierarchy management (lifecycle, orientation, low-memory conditions) • Manages the content of one screen or of one part of a screen • Subclass to create custom view controllers
  • 8. What is a container? • Is an object managing a set of view controllers • In general used as-is, not meant to be subclassed • Helps managing the view controller hierarchy, i.e. the application workflow... • ... but only the workflow it implements
  • 9. UINavigationController • Navigation bar at the top • Push from right / left animations only
  • 10. UITabBarController • Tabs at the bottom • Must be application root VC or root in modal
  • 11. Criticism • Few built-in containers... • ... i.e. few well-supported workflows (navigation, tabs, modal, split content) • This is annoying on iPad (emphasis on creative interfaces, use of both hands), less on iPhone (skinned interfaces, one hand usually suffices)
  • 12. An iPad shopping application • Application to buy items from some online store on the iPad • Three people involved: • Product owner: Requirements • UI / UX designer: Design interaction • Programmer: Write the application
  • 13. Product owner • “I want the customer to be able to browse products and their descriptions” • “I want the customer to be able to add items to its cart” • “I want the customer to be able to access its cart and to submit an order”
  • 14. UI / UX designer (1)
  • 15. UI / UX designer (2)
  • 16. Programmer:VCs ShoppingViewController Product- ProductList- ViewController ViewController ShoppingViewController ShoppingViewController CartItemList- Stayin BillingInformation- ViewController carttab ViewController
  • 17. Programmer: Workflow retains VC and addSubview: ShoppingViewController ProductListViewController presentModalViewController: showShop: showDetails: animated: showCart: retains VC and ProductViewController addSubview: checkout: close: CartItemListViewController goBackwards: dismissModalViewController showDetails: Animated: presentModalViewController: checkout: animated: BillingInformationViewController retains VC and addSubview: submitOrder: goBackwards: IBAction
  • 18. Demo
  • 19. Criticism • No transparency when displaying product details • ShoppingViewController manages almost all application workflow • Seems to work, but does it REALLY work?
  • 20. Analyzing the shopping application • Use VCs logging all lifecycle and rotation events • Navigate the application • Rotate the device • Simulate memory warnings • Check the logs
  • 21. Log archaeology (1) • Click on Shop tab, then on Cart tab: • Show product details
  • 22. Log archaeology (2) retains VC and addSubview: ShoppingViewController ProductListViewController presentModalViewController: showShop: showDetails: animated: showCart: retains VC and ProductViewController addSubview: checkout: close: CartItemListViewController goBackwards: dismissModalViewController showDetails: Animated: presentModalViewController: checkout: animated: BillingInformationViewController retains VC and addSubview: submitOrder: View lifecycle and rotation OK goBackwards: View lifecycle and rotation KO
  • 23. Conclusion • Creating a view controllers hierarchy using addSubview: leads to incorrect behavior • View lifecycle and rotation events are not emitted automagically • The root and modal view controllers have a proper behavior Always use containers to manage your view controller hierarchy
  • 24. Custom containers • Custom containers are needed for more creative workflows • Using custom containers must be easy... • ... but writing custom containers is hard
  • 25. Elementary containers • Writing containers is hard... • ... so write few correct elementary containers... • ... and combine them to create new workflows... • ... or even new containers!
  • 26. Placeholders • Embed some other view controllers • Display VCs simultaneously • Can display other UI elements VC1 VC1 VC1 VC3 VC2 VC2 1-placeholder 2-placeholder 3-placeholder
  • 27. Stack • Manage a stack of view controllers • Usually only display the top VC • Fill the whole view, no other UI elements VC4 VC1 VC1 VC1
  • 28. Combinations (1) VC1 = custom UITabBarController 1-placeholder
  • 29. Combinations (2) + VC4 VC1 VC1 VC1 VC1 stack 1-placeholder = custom UINavigationController
  • 30. Combinations (3) VC1 VC2 2-placeholder = resizable UISplitViewController
  • 31. Implementation • CoconutKit (https://github.com/defagos/ CoconutKit): • HLSPlaceholderViewController • HLSStackController • Technical details: http://subjective-objective- c.blogspot.com/2011/08/writing-high- quality-view-controller.html
  • 32. Demo
  • 33. iShopping revisited ShoppingViewController(PH) stack Product- ProductList- ViewController ViewController ShoppingViewController(PH) ShoppingViewController(PH) CartItemList- BillingInformation- stack ViewController ViewController
  • 34. Buggy workflow retains VC and addSubview: ShoppingViewController ProductListViewController presentModalViewController: showShop: showDetails: animated: showCart: retains VC and ProductViewController addSubview: checkout: close: CartItemListViewController goBackwards: dismissModalViewController showDetails: Animated: presentModalViewController: checkout: animated: BillingInformationViewController retains VC and addSubview: submitOrder: goBackwards:
  • 35. Fixed workflow ShoppingViewController (stack root placeholder) ProductListViewController pushViewController: showShop: showDetails: setInsetViewController: showCart: ProductViewController pushViewController: close: popViewController: § CartItemListViewController (stack root) showDetails: checkout: BillingInformationViewController pushViewController: submitOrder: goBackwards: StackController popViewController: StackController
  • 36. Demo
  • 37. Log archaeology • Click on Cart tab when on Shop tab: • Show product details
  • 38. Summary • Never ever display a view controller by adding its view as subview directly • Design your view controller workflow, and manage it using view controller containers • Use the view controller lifecycle correctly to insert your code where most appropriate
  • 39. Thanks for your attention