the life and times of uiviewcontroller
TRANSCRIPT
![Page 1: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/1.jpg)
The Life and Times of UIViewController
Brandon Alexander
Wednesday, September 18, 13
![Page 2: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/2.jpg)
Who am IiOS Developer, Author, Speaker
Email: [email protected]
GTalk: [email protected]
Twitter/ADN: @balexander
Wednesday, September 18, 13
![Page 3: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/3.jpg)
Design Pattern
A general reusable solution to a commonly occurring problem within a given context in software design
Wednesday, September 18, 13
![Page 4: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/4.jpg)
MVC
• Model
• View
• Controller
Wednesday, September 18, 13
![Page 5: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/5.jpg)
Controller
• Mediates between view and model
• Subclasses UIViewController
Wednesday, September 18, 13
![Page 6: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/6.jpg)
Agenda
Wednesday, September 18, 13
![Page 7: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/7.jpg)
Agenda
• UIViewController Lifecycle
Wednesday, September 18, 13
![Page 8: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/8.jpg)
Agenda
• UIViewController Lifecycle
• Presenting other view controllers
Wednesday, September 18, 13
![Page 9: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/9.jpg)
Agenda
• UIViewController Lifecycle
• Presenting other view controllers
• View Controller Containment
Wednesday, September 18, 13
![Page 10: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/10.jpg)
Agenda
• UIViewController Lifecycle
• Presenting other view controllers
• View Controller Containment
• [REDACTED]
Wednesday, September 18, 13
![Page 11: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/11.jpg)
Lifecycle
• Creation
• Interaction
• Destruction
Wednesday, September 18, 13
![Page 12: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/12.jpg)
Creation
• NIB (or XIB)
• Storyboard
• In Code
Wednesday, September 18, 13
![Page 13: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/13.jpg)
NIB
- (id) initWithNibName:(NSString *)n bundle:(NSBundle *)b
Wednesday, September 18, 13
![Page 14: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/14.jpg)
Storyboard
- (instancetype) initWithCoder:(NSCoder *)c
Wednesday, September 18, 13
![Page 15: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/15.jpg)
Code
- (instancetype) init- (instancetype) initWith...
- (void) loadView
Wednesday, September 18, 13
![Page 16: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/16.jpg)
Creation Tips
• Wait for viewDidLoad for some startup items
• Don’t reference vc.view before viewDidLoad is called
• Use -[UIVC isViewLoaded]
• Use UIGestureRecognizer when possible
Wednesday, September 18, 13
![Page 17: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/17.jpg)
Interaction
• Respond to user actions
• Delegate/Data Source Methods
• Respond to rotation events
• Respond to application notifications
• Navigate to other view controllers
Wednesday, September 18, 13
![Page 18: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/18.jpg)
Important Methods- (void) viewWillAppear:(BOOL)animated- (void) viewDidAppear:(BOOL)animated
- (void) viewWillDisappear:(BOOL)animated- (void) viewDidDisappear:(BOOL)animated
- (void) viewWillLayoutSubviews- (void) viewDidLayoutSubviews
- (void) didReceiveMemoryWarning
Wednesday, September 18, 13
![Page 19: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/19.jpg)
Rotation Support
• Info.plist shows all supported orientations
• Implement proper methods to support each orientation in each view controller
Wednesday, September 18, 13
![Page 20: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/20.jpg)
Interaction Tips
• Split Data Source methods out to helper objects
• Keep the focus of the class in mind
Wednesday, September 18, 13
![Page 21: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/21.jpg)
Destruction
• Implement dealloc when necessary
• Many existing UIKit classes aren’t ARCified
• Set delegate/dataSource properties to nil
Wednesday, September 18, 13
![Page 22: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/22.jpg)
Presenting View Controllers
! //Create VC! UIViewController *vc = [UIViewController new];!! //Configure! vc.modalPresentationStyle = UIModalPresentationFormSheet;! vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;!! //Present! [self presentViewController:vc animated:YES completion:nil];
Wednesday, September 18, 13
![Page 23: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/23.jpg)
Dismissing Presented View Controllers
• Multiple Approaches
• Delegation
• Post to the responder chain
• Don’t have a view controller dismiss itself
Wednesday, September 18, 13
![Page 24: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/24.jpg)
View Controller Containment
• Keeps View Controller hierarchy the same as the view hierarchy
• Helps solve the Massive View Controller anti-pattern
Wednesday, September 18, 13
![Page 25: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/25.jpg)
Adding a Child VC
- (void) displayContentController: (UIViewController*) content {! [self addChildViewController:content];! content.view.frame = [self frameForContentController];! [self.view addSubview:self.currentClientView];! [content didMoveToParentViewController:self];}
Wednesday, September 18, 13
![Page 26: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/26.jpg)
Removing a child VC
- (void) hideContentController: (UIViewController*) content {! [content willMoveToParentViewController:nil];! [content.view removeFromSuperview];! [content removeFromParentViewController];}
Wednesday, September 18, 13
![Page 27: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/27.jpg)
Other Containment Methods
- (BOOL) shouldAutomaticallyForwardAppearanceMethods- (BOOL) shouldAutomaticallyForwardRotationMethods
Wednesday, September 18, 13
![Page 28: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/28.jpg)
iOS 7
• Custom View Controller Transitions
• Different layout paradigms
• -automaticallyAdjustsScrollViewInsets
• UIKit Dynamics
Wednesday, September 18, 13
![Page 29: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/29.jpg)
Architecture
• Avoid Massive View Controllers
• Centralize networking code
• Controllers don’t have to be view controllers
Wednesday, September 18, 13
![Page 30: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/30.jpg)
Other Tricks
• Use nil-target actions in views
• Lazy load in container view controllers
• Don’t bother cleaning up in will(Dis)Appear
Wednesday, September 18, 13
![Page 31: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/31.jpg)
Resources
• View Controller Programming Guide
• About Cocoa Auto Layout
• iOS 7 UI Transition Guide
Wednesday, September 18, 13
![Page 32: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/32.jpg)
Questions
Wednesday, September 18, 13
![Page 33: The Life and Times of UIViewController](https://reader034.vdocuments.site/reader034/viewer/2022052522/554f59d3b4c905c8088b4596/html5/thumbnails/33.jpg)
Contact
Email: [email protected]
GTalk: [email protected]
Twitter/ADN: @balexander
Wednesday, September 18, 13