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. 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. 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. Communication and MVC
⢠How should objects communicate?
⢠Which objects know about one another?
Friday, January 22, 2010 16
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. 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. 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
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. 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. 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. UIViewController
⢠Basic building block
⢠Manages a screenful of content
⢠Subclass to add your application logic
Friday, January 22, 2010 24
40. UIViewController
⢠Basic building block
⢠Manages a screenful of content
⢠Subclass to add your application logic
Friday, January 22, 2010 24
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. â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. â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
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. 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. 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
49. The âViewâ in âView Controllerâ
⢠UIViewController superclass has a view property
â @property (retain) UIView *view;
Friday, January 22, 2010 27
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. 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. When to call -loadView?
Friday, January 22, 2010 28
53. When to call -loadView?
⢠Donât do it!
Friday, January 22, 2010 28
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. 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
57. Creating Your View in Code
⢠Override -loadView
â Never call this directly
// Subclass of UIViewController
- (void)loadView
{
}
Friday, January 22, 2010 29
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. 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. 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
62. Creating Your View with Interface Builder
⢠Lay out a view in Interface Builder
Friday, January 22, 2010 30
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. 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. 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. Demo:
View Controllers with IB
Friday, January 22, 2010 31
70. View Controller Lifecycle
- (void)viewDidLoad
{
// Your view has been loaded
// Customize it here if needed
view.someWeirdProperty = YES;
}
Friday, January 22, 2010 33
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
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. 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. Demo:
Loading & Saving Data
Friday, January 22, 2010 37
77. More View Controller Hooks
⢠Automatically rotating your user interface
⢠Low memory warnings
Friday, January 22, 2010 38