The Life and Times of UIViewController

915
-1

Published on

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
915
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
24
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

The Life and Times of UIViewController

  1. 1. The Life and Times of UIViewController Brandon Alexander Wednesday, September 18, 13
  2. 2. Who am I iOS Developer,Author, Speaker Email: brandon.alexander@gmail.com GTalk: brandon.alexander@gmail.com Twitter/ADN: @balexander Wednesday, September 18, 13
  3. 3. Design Pattern A general reusable solution to a commonly occurring problem within a given context in software design Wednesday, September 18, 13
  4. 4. MVC • Model • View • Controller Wednesday, September 18, 13
  5. 5. Controller • Mediates between view and model • Subclasses UIViewController Wednesday, September 18, 13
  6. 6. Agenda Wednesday, September 18, 13
  7. 7. Agenda • UIViewController Lifecycle Wednesday, September 18, 13
  8. 8. Agenda • UIViewController Lifecycle • Presenting other view controllers Wednesday, September 18, 13
  9. 9. Agenda • UIViewController Lifecycle • Presenting other view controllers • View Controller Containment Wednesday, September 18, 13
  10. 10. Agenda • UIViewController Lifecycle • Presenting other view controllers • View Controller Containment • [REDACTED] Wednesday, September 18, 13
  11. 11. Lifecycle • Creation • Interaction • Destruction Wednesday, September 18, 13
  12. 12. Creation • NIB (or XIB) • Storyboard • In Code Wednesday, September 18, 13
  13. 13. NIB - (id) initWithNibName:(NSString *)n bundle:(NSBundle *)b Wednesday, September 18, 13
  14. 14. Storyboard - (instancetype) initWithCoder:(NSCoder *)c Wednesday, September 18, 13
  15. 15. Code - (instancetype) init - (instancetype) initWith... - (void) loadView Wednesday, September 18, 13
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 19. Rotation Support • Info.plist shows all supported orientations • Implement proper methods to support each orientation in each view controller Wednesday, September 18, 13
  20. 20. Interaction Tips • Split Data Source methods out to helper objects • Keep the focus of the class in mind Wednesday, September 18, 13
  21. 21. Destruction • Implement dealloc when necessary • Many existing UIKit classes aren’t ARCified • Set delegate/dataSource properties to nil Wednesday, September 18, 13
  22. 22. PresentingView 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
  23. 23. Dismissing Presented View Controllers • Multiple Approaches • Delegation • Post to the responder chain • Don’t have a view controller dismiss itself Wednesday, September 18, 13
  24. 24. View Controller Containment • KeepsView Controller hierarchy the same as the view hierarchy • Helps solve the MassiveView Controller anti-pattern Wednesday, September 18, 13
  25. 25. Adding a ChildVC - (void) displayContentController: (UIViewController*) content { ! [self addChildViewController:content]; ! content.view.frame = [self frameForContentController]; ! [self.view addSubview:self.currentClientView]; ! [content didMoveToParentViewController:self]; } Wednesday, September 18, 13
  26. 26. Removing a childVC - (void) hideContentController: (UIViewController*) content { ! [content willMoveToParentViewController:nil]; ! [content.view removeFromSuperview]; ! [content removeFromParentViewController]; } Wednesday, September 18, 13
  27. 27. Other Containment Methods - (BOOL) shouldAutomaticallyForwardAppearanceMethods - (BOOL) shouldAutomaticallyForwardRotationMethods Wednesday, September 18, 13
  28. 28. iOS 7 • CustomView Controller Transitions • Different layout paradigms • -automaticallyAdjustsScrollViewInsets • UIKit Dynamics Wednesday, September 18, 13
  29. 29. Architecture • Avoid MassiveView Controllers • Centralize networking code • Controllers don’t have to be view controllers Wednesday, September 18, 13
  30. 30. 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
  31. 31. Resources • View Controller Programming Guide • About Cocoa Auto Layout • iOS 7 UI Transition Guide Wednesday, September 18, 13
  32. 32. Questions Wednesday, September 18, 13
  33. 33. Contact Email: brandon.alexander@gmail.com GTalk: brandon.alexander@gmail.com Twitter/ADN: @balexander Wednesday, September 18, 13
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×