06 View Controllers

1,430 views

Published on

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

No Downloads
Views
Total views
1,430
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
52
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

06 View Controllers

  1. 1. CS193P - Lecture 6 iPhone Application Development Designing iPhone Applications Model-View-Controller (Why and How?) View Controllers Friday, January 22, 2010 1
  2. 2. Announcements • Questions about Views? • Friday’s optional section... ■ Extended Office Hours ■ Gates 360, 3:30 - 5pm Friday, January 22, 2010 2
  3. 3. Today’s Topics • Designing iPhone Applications • Model-View-Controller (Why and How?) • View Controllers Friday, January 22, 2010 3
  4. 4. Designing iPhone Applications Friday, January 22, 2010 4
  5. 5. Two Flavors of Mail Friday, January 22, 2010 5
  6. 6. Organizing Content Friday, January 22, 2010 6
  7. 7. Organizing Content Friday, January 22, 2010 6
  8. 8. Organizing Content Friday, January 22, 2010 6
  9. 9. Organizing Content Friday, January 22, 2010 6
  10. 10. Organizing Content • Focus on your user’s data Friday, January 22, 2010 6
  11. 11. Organizing Content • Focus on your user’s data • One thing at a time Friday, January 22, 2010 6
  12. 12. Organizing Content • Focus on your user’s data • One thing at a time • Screenfuls of content Friday, January 22, 2010 6
  13. 13. Patterns for Organizing Content Friday, January 22, 2010 7
  14. 14. Patterns for Organizing Content Navigation Bar Friday, January 22, 2010 7
  15. 15. Patterns for Organizing Content Navigation Bar Tab Bar Friday, January 22, 2010 7
  16. 16. Navigation Bar • Hierarchy of content • Drill down into greater detail Friday, January 22, 2010 8
  17. 17. Navigation Bar • Hierarchy of content • Drill down into greater detail Friday, January 22, 2010 8
  18. 18. Tab Bar • Self-contained modes Friday, January 22, 2010 9
  19. 19. Tab Bar • Self-contained modes Friday, January 22, 2010 9
  20. 20. A Screenful of Content • Slice of your application • Views, data, logic Friday, January 22, 2010 10
  21. 21. Parts of a Screenful Model View Friday, January 22, 2010 11
  22. 22. Parts of a Screenful Model View Controller Friday, January 22, 2010 11
  23. 23. Parts of a Screenful Model View Controller Friday, January 22, 2010 12
  24. 24. Model-View-Controller (Why and How?) Friday, January 22, 2010 13
  25. 25. Why Model-View-Controller? • Ever used the word “spaghetti” to describe code? • Clear responsibilities make things easier to maintain • Avoid having one monster class that does everything Friday, January 22, 2010 14
  26. 26. Why Model-View-Controller? • Ever used the word “spaghetti” to describe code? • Clear responsibilities make things easier to maintain • Avoid having one monster class that does everything Friday, January 22, 2010 14
  27. 27. Why Model-View-Controller? • Separating responsibilites also leads to reusability • By minimizing dependencies, you can take a model or view class you’ve already written and use it elsewhere • Think of ways to write less code Friday, January 22, 2010 15
  28. 28. Communication and MVC • How should objects communicate? • Which objects know about one another? Friday, January 22, 2010 16
  29. 29. Communication and MVC • How should objects communicate? • Which objects know about one another? • Model • Example: Polygon class • Not aware of views or controllers • Typically the most reusable • Communicate generically using... Model ■ Key-value observing ■ Notifications Friday, January 22, 2010 17
  30. 30. Communication and MVC • How should objects communicate? • Which objects know about one another? • View • Example: PolygonView class • Not aware of controllers, may be aware of relevant model objects • Also tends to be reusable View • Communicate with controller using... ■ Target-action ■ Delegation Friday, January 22, 2010 18
  31. 31. Communication and MVC • How should objects communicate? • Which objects know about one another? • Controller • Knows about model and view objects • The brains of the operation • Manages relationships and data flow • Typically app-specific, Controller so rarely reusable Friday, January 22, 2010 19
  32. 32. Communication and MVC Model View Controller Friday, January 22, 2010 20
  33. 33. Communication and MVC Model View Controller Friday, January 22, 2010 20
  34. 34. Communication and MVC Model View KVO, target-action, notifications delegation Controller Friday, January 22, 2010 20
  35. 35. View Controllers Friday, January 22, 2010 21
  36. 36. Problem: Managing a Screenful • Controller manages views, data and application logic • Apps are made up of many of these • Would be nice to have a well-defined starting point ■ A la UIView for views ■ Common language for talking about controllers Friday, January 22, 2010 22
  37. 37. Problem: Building Typical Apps • Some application flows are very common ■ Navigation-based ■ Tab bar-based ■ Combine the two • Don’t reinvent the wheel • Plug individual screens together to build an app Friday, January 22, 2010 23
  38. 38. UIViewController • Basic building block • Manages a screenful of content • Subclass to add your application logic View Controller Views Data Logic Friday, January 22, 2010 24
  39. 39. UIViewController • Basic building block • Manages a screenful of content • Subclass to add your application logic Friday, January 22, 2010 24
  40. 40. UIViewController • Basic building block • Manages a screenful of content • Subclass to add your application logic Friday, January 22, 2010 24
  41. 41. “Your” and “Our” View Controllers • Create your own UIViewController subclass for each screenful • Plug them together using existing composite view controllers Friday, January 22, 2010 25
  42. 42. “Your” and “Our” View Controllers • Create your own UIViewController subclass for each screenful • Plug them together using existing composite view controllers View Controller Navigation Controller View Controller View Controller Friday, January 22, 2010 25
  43. 43. “Your” and “Our” View Controllers • Create your own UIViewController subclass for each screenful • Plug them together using existing composite view controllers View Controller Tab Bar View Controller Controller View Controller Friday, January 22, 2010 25
  44. 44. Your View Controller Subclass Friday, January 22, 2010 26
  45. 45. Your View Controller Subclass #import <UIKit/UIKit.h> @interface MyViewController : UIViewController { // A view controller will usually // manage views and data NSMutableArray *myData; UILabel *myLabel; } Friday, January 22, 2010 26
  46. 46. Your View Controller Subclass #import <UIKit/UIKit.h> @interface MyViewController : UIViewController { // A view controller will usually // manage views and data NSMutableArray *myData; UILabel *myLabel; } // Expose some of its contents to clients @property (readonly) NSArray *myData; Friday, January 22, 2010 26
  47. 47. Your View Controller Subclass #import <UIKit/UIKit.h> @interface MyViewController : UIViewController { // A view controller will usually // manage views and data NSMutableArray *myData; UILabel *myLabel; } // Expose some of its contents to clients @property (readonly) NSArray *myData; // And respond to actions - (void)doSomeAction:(id)sender; Friday, January 22, 2010 26
  48. 48. The “View” in “View Controller” Friday, January 22, 2010 27
  49. 49. The “View” in “View Controller” • UIViewController superclass has a view property ■ @property (retain) UIView *view; Friday, January 22, 2010 27
  50. 50. The “View” in “View Controller” • UIViewController superclass has a view property ■ @property (retain) UIView *view; • Loads lazily ■ On demand when requested ■ Can be purged on demand as well (low memory) Friday, January 22, 2010 27
  51. 51. The “View” in “View Controller” • UIViewController superclass has a view property ■ @property (retain) UIView *view; • Loads lazily ■ On demand when requested ■ Can be purged on demand as well (low memory) • Sizing and positioning the view? ■ Depends on where it’s being used ■ Don’t make assumptions, be flexible Friday, January 22, 2010 27
  52. 52. When to call -loadView? Friday, January 22, 2010 28
  53. 53. When to call -loadView? • Don’t do it! Friday, January 22, 2010 28
  54. 54. When to call -loadView? • Don’t do it! • Cocoa tends to embrace a lazy philosophy ■ Call -release instead of -dealloc ■ Call -setNeedsDisplay instead of -drawRect: Friday, January 22, 2010 28
  55. 55. When to call -loadView? • Don’t do it! • Cocoa tends to embrace a lazy philosophy ■ Call -release instead of -dealloc ■ Call -setNeedsDisplay instead of -drawRect: • Allows work to be deferred or coalesced ■ Performance! Friday, January 22, 2010 28
  56. 56. Creating Your View in Code Friday, January 22, 2010 29
  57. 57. Creating Your View in Code • Override -loadView ■ Never call this directly // Subclass of UIViewController - (void)loadView { } Friday, January 22, 2010 29
  58. 58. Creating Your View in Code • Override -loadView ■ Never call this directly • Create your views // Subclass of UIViewController - (void)loadView { MyView *myView = [[MyView alloc] initWithFrame:frame]; [myView release]; } Friday, January 22, 2010 29
  59. 59. Creating Your View in Code • Override -loadView ■ Never call this directly • Create your views • Set the view property // Subclass of UIViewController - (void)loadView { MyView *myView = [[MyView alloc] initWithFrame:frame]; self.view = myView; // The view controller now owns the view [myView release]; } Friday, January 22, 2010 29
  60. 60. Creating Your View in Code • Override -loadView ■ Never call this directly • Create your views • Set the view property • Create view controller with -init // Subclass of UIViewController - (void)loadView { MyView *myView = [[MyView alloc] initWithFrame:frame]; self.view = myView; // The view controller now owns the view [myView release]; } Friday, January 22, 2010 29
  61. 61. Creating Your View with Interface Builder Friday, January 22, 2010 30
  62. 62. Creating Your View with Interface Builder • Lay out a view in Interface Builder Friday, January 22, 2010 30
  63. 63. Creating Your View with Interface Builder • Lay out a view in Interface Builder • File’s owner is view controller class Friday, January 22, 2010 30
  64. 64. Creating Your View with Interface Builder • Lay out a view in Interface Builder • File’s owner is view controller class • Hook up view outlet Friday, January 22, 2010 30
  65. 65. Creating Your View with Interface Builder • Lay out a view in Interface Builder • File’s owner is view controller class • Hook up view outlet • Create view controller with -initWithNibName:bundle: Friday, January 22, 2010 30
  66. 66. Demo: View Controllers with IB Friday, January 22, 2010 31
  67. 67. View Controller Lifecycle Friday, January 22, 2010 32
  68. 68. View Controller Lifecycle - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle { if (self == [super init...]) { // Perform initial setup, nothing view-related myData = [[NSMutableArray alloc] init]; self.title = @“Foo”; } return self; } Friday, January 22, 2010 32
  69. 69. View Controller Lifecycle Friday, January 22, 2010 33
  70. 70. View Controller Lifecycle - (void)viewDidLoad { // Your view has been loaded // Customize it here if needed view.someWeirdProperty = YES; } Friday, January 22, 2010 33
  71. 71. View Controller Lifecycle Friday, January 22, 2010 34
  72. 72. View Controller Lifecycle - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // Your view is about to show on the screen [self beginLoadingDataFromTheWeb]; [self startShowingLoadingProgress]; } Friday, January 22, 2010 34
  73. 73. View Controller Lifecycle Friday, January 22, 2010 35
  74. 74. View Controller Lifecycle - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // Your view is about to leave the screen [self rememberScrollPosition]; [self saveDataToDisk]; } Friday, January 22, 2010 35
  75. 75. Loading & Saving Data • Lots of options out there, depends on what you need ■ NSUserDefaults ■ Property lists ■ CoreData ■ SQLite ■ Web services • Covering in greater depth in Lecture 9 on 4/29 Friday, January 22, 2010 36
  76. 76. Demo: Loading & Saving Data Friday, January 22, 2010 37
  77. 77. More View Controller Hooks • Automatically rotating your user interface • Low memory warnings Friday, January 22, 2010 38
  78. 78. Supporting Interface Rotation Friday, January 22, 2010 39
  79. 79. Supporting Interface Rotation - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { // This view controller only supports portrait return (interfaceOrientation == UIInterfaceOrientationPortrait); } Friday, January 22, 2010 39
  80. 80. Supporting Interface Rotation Friday, January 22, 2010 40
  81. 81. Supporting Interface Rotation - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { // This view controller supports all orientations // except for upside-down. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } Friday, January 22, 2010 40
  82. 82. Demo: Rotating Your Interface Friday, January 22, 2010 41
  83. 83. Autoresizing Your Views Friday, January 22, 2010 42
  84. 84. Autoresizing Your Views view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; Friday, January 22, 2010 42
  85. 85. Autoresizing Your Views view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; Friday, January 22, 2010 42
  86. 86. Autoresizing Your Views view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; Friday, January 22, 2010 42
  87. 87. Autoresizing Your Views view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; Friday, January 22, 2010 42
  88. 88. Questions? Friday, January 22, 2010 43

×