06 view controllers

88
CS193P - Lecture 6 iPhone Application Development Designing iPhone Applications Model-View-Controller (Why and How?) View Controllers 1 Friday, January 22, 2010

Upload: mahmoud

Post on 28-Jan-2015

107 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 06 View Controllers

CS193P - Lecture 6iPhone Application Development

Designing iPhone ApplicationsModel-View-Controller (Why and How?)View Controllers

1Friday, January 22, 2010

Page 2: 06 View Controllers

Announcements• Questions about Views?• Friday’s optional section...

■ Extended Office Hours■ Gates 360, 3:30 - 5pm

2Friday, January 22, 2010

Page 3: 06 View Controllers

Today’s Topics• Designing iPhone Applications• Model-View-Controller (Why and How?)• View Controllers

3Friday, January 22, 2010

Page 4: 06 View Controllers

Designing iPhone Applications

4Friday, January 22, 2010

Page 5: 06 View Controllers

Two Flavors of Mail

5Friday, January 22, 2010

Page 6: 06 View Controllers

Organizing Content

6Friday, January 22, 2010

Page 7: 06 View Controllers

Organizing Content

6Friday, January 22, 2010

Page 8: 06 View Controllers

Organizing Content

6Friday, January 22, 2010

Page 9: 06 View Controllers

Organizing Content

6Friday, January 22, 2010

Page 10: 06 View Controllers

Organizing Content• Focus on your user’s data

6Friday, January 22, 2010

Page 11: 06 View Controllers

Organizing Content• Focus on your user’s data• One thing at a time

6Friday, January 22, 2010

Page 12: 06 View Controllers

Organizing Content• Focus on your user’s data• One thing at a time• Screenfuls of content

6Friday, January 22, 2010

Page 13: 06 View Controllers

Patterns for Organizing Content

7Friday, January 22, 2010

Page 14: 06 View Controllers

Patterns for Organizing Content

Navigation Bar

7Friday, January 22, 2010

Page 15: 06 View Controllers

Patterns for Organizing Content

Navigation Bar Tab Bar

7Friday, January 22, 2010

Page 16: 06 View Controllers

Navigation Bar• Hierarchy of content• Drill down into greater detail

8Friday, January 22, 2010

Page 17: 06 View Controllers

Navigation Bar• Hierarchy of content• Drill down into greater detail

8Friday, January 22, 2010

Page 18: 06 View Controllers

Tab Bar• Self-contained modes

9Friday, January 22, 2010

Page 19: 06 View Controllers

Tab Bar• Self-contained modes

9Friday, January 22, 2010

Page 20: 06 View Controllers

A Screenful of Content• Slice of your application• Views, data, logic

10Friday, January 22, 2010

Page 21: 06 View Controllers

Model View

Parts of a Screenful

11Friday, January 22, 2010

Page 22: 06 View Controllers

Model View

Parts of a Screenful

Controller

11Friday, January 22, 2010

Page 23: 06 View Controllers

Parts of a Screenful

Model View

Controller

12Friday, January 22, 2010

Page 24: 06 View Controllers

Model-View-Controller(Why and How?)

13Friday, January 22, 2010

Page 25: 06 View Controllers

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

14Friday, January 22, 2010

Page 26: 06 View Controllers

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

14Friday, January 22, 2010

Page 27: 06 View Controllers

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

15Friday, January 22, 2010

Page 28: 06 View Controllers

Communication and MVC• How should objects communicate?• Which objects know about one another?

16Friday, January 22, 2010

Page 29: 06 View Controllers

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...

■ Key-value observing ■ Notifications

Model

17Friday, January 22, 2010

Page 30: 06 View Controllers

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• Communicate with controller using...

■ Target-action■ Delegation

View

18Friday, January 22, 2010

Page 31: 06 View Controllers

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,

so rarely reusableController

19Friday, January 22, 2010

Page 32: 06 View Controllers

Communication and MVC

Model View

Controller

20Friday, January 22, 2010

Page 33: 06 View Controllers

Communication and MVC

Model View

Controller

20Friday, January 22, 2010

Page 34: 06 View Controllers

Communication and MVC

Model View

Controller

target-action,delegation

KVO,notifications

20Friday, January 22, 2010

Page 35: 06 View Controllers

View Controllers

21Friday, January 22, 2010

Page 36: 06 View Controllers

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

22Friday, January 22, 2010

Page 37: 06 View Controllers

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

23Friday, January 22, 2010

Page 38: 06 View Controllers

UIViewController• Basic building block• Manages a screenful of content• Subclass to add your application logic

LogicDataViewsView Controller

24Friday, January 22, 2010

Page 39: 06 View Controllers

UIViewController• Basic building block• Manages a screenful of content• Subclass to add your application logic

24Friday, January 22, 2010

Page 40: 06 View Controllers

UIViewController• Basic building block• Manages a screenful of content• Subclass to add your application logic

24Friday, January 22, 2010

Page 41: 06 View Controllers

“Your” and “Our” View Controllers• Create your own UIViewController subclass for each screenful• Plug them together using existing composite view controllers

25Friday, January 22, 2010

Page 42: 06 View Controllers

“Your” and “Our” View Controllers• Create your own UIViewController subclass for each screenful• Plug them together using existing composite view controllers

View Controller

View Controller

View Controller

NavigationController

25Friday, January 22, 2010

Page 43: 06 View Controllers

“Your” and “Our” View Controllers• Create your own UIViewController subclass for each screenful• Plug them together using existing composite view controllers

View Controller

View Controller

View Controller

Tab BarController

25Friday, January 22, 2010

Page 44: 06 View Controllers

Your View Controller Subclass

26Friday, January 22, 2010

Page 45: 06 View Controllers

Your View Controller Subclass#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController {// A view controller will usually// manage views and dataNSMutableArray *myData;UILabel *myLabel;

}

26Friday, January 22, 2010

Page 46: 06 View Controllers

Your View Controller Subclass#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController {// A view controller will usually// manage views and dataNSMutableArray *myData;UILabel *myLabel;

}

// Expose some of its contents to clients@property (readonly) NSArray *myData;

26Friday, January 22, 2010

Page 47: 06 View Controllers

Your View Controller Subclass#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController {// A view controller will usually// manage views and dataNSMutableArray *myData;UILabel *myLabel;

}

// Expose some of its contents to clients@property (readonly) NSArray *myData;

// And respond to actions- (void)doSomeAction:(id)sender;

26Friday, January 22, 2010

Page 48: 06 View Controllers

The “View” in “View Controller”

27Friday, January 22, 2010

Page 49: 06 View Controllers

The “View” in “View Controller”• UIViewController superclass has a view property

■ @property (retain) UIView *view;

27Friday, January 22, 2010

Page 50: 06 View Controllers

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)

27Friday, January 22, 2010

Page 51: 06 View Controllers

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

27Friday, January 22, 2010

Page 52: 06 View Controllers

When to call -loadView?

28Friday, January 22, 2010

Page 53: 06 View Controllers

When to call -loadView?• Don’t do it!

28Friday, January 22, 2010

Page 54: 06 View Controllers

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:

28Friday, January 22, 2010

Page 55: 06 View Controllers

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!

28Friday, January 22, 2010

Page 56: 06 View Controllers

Creating Your View in Code

29Friday, January 22, 2010

Page 57: 06 View Controllers

Creating Your View in Code• Override -loadView

■ Never call this directly

// Subclass of UIViewController- (void)loadView{

}

29Friday, January 22, 2010

Page 58: 06 View Controllers

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];

29Friday, January 22, 2010

Page 59: 06 View Controllers

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];

[myView release];self.view = myView; // The view controller now owns the view

29Friday, January 22, 2010

Page 60: 06 View Controllers

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];

[myView release];self.view = myView; // The view controller now owns the view

29Friday, January 22, 2010

Page 61: 06 View Controllers

Creating Your View with Interface Builder

30Friday, January 22, 2010

Page 62: 06 View Controllers

Creating Your View with Interface Builder• Lay out a view in Interface Builder

30Friday, January 22, 2010

Page 63: 06 View Controllers

Creating Your View with Interface Builder• Lay out a view in Interface Builder• File’s owner is view controller class

30Friday, January 22, 2010

Page 64: 06 View Controllers

Creating Your View with Interface Builder• Lay out a view in Interface Builder• File’s owner is view controller class• Hook up view outlet

30Friday, January 22, 2010

Page 65: 06 View Controllers

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:

30Friday, January 22, 2010

Page 66: 06 View Controllers

Demo:View Controllers with IB

31Friday, January 22, 2010

Page 67: 06 View Controllers

View Controller Lifecycle

32Friday, January 22, 2010

Page 68: 06 View Controllers

View Controller Lifecycle- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle{

if (self == [super init...]) {// Perform initial setup, nothing view-relatedmyData = [[NSMutableArray alloc] init];self.title = @“Foo”;

}return self;

}

32Friday, January 22, 2010

Page 69: 06 View Controllers

View Controller Lifecycle

33Friday, January 22, 2010

Page 70: 06 View Controllers

View Controller Lifecycle- (void)viewDidLoad{

// Your view has been loaded// Customize it here if neededview.someWeirdProperty = YES;

}

33Friday, January 22, 2010

Page 71: 06 View Controllers

View Controller Lifecycle

34Friday, January 22, 2010

Page 72: 06 View Controllers

View Controller Lifecycle- (void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

// Your view is about to show on the screen[self beginLoadingDataFromTheWeb];[self startShowingLoadingProgress];

}

34Friday, January 22, 2010

Page 73: 06 View Controllers

View Controller Lifecycle

35Friday, January 22, 2010

Page 74: 06 View Controllers

View Controller Lifecycle- (void)viewWillDisappear:(BOOL)animated{

[super viewWillDisappear:animated];

// Your view is about to leave the screen[self rememberScrollPosition];[self saveDataToDisk];

}

35Friday, January 22, 2010

Page 75: 06 View Controllers

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

36Friday, January 22, 2010

Page 76: 06 View Controllers

Demo:Loading & Saving Data

37Friday, January 22, 2010

Page 77: 06 View Controllers

More View Controller Hooks• Automatically rotating your user interface• Low memory warnings

38Friday, January 22, 2010

Page 78: 06 View Controllers

Supporting Interface Rotation

39Friday, January 22, 2010

Page 79: 06 View Controllers

Supporting Interface Rotation- (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation{

// This view controller only supports portraitreturn (interfaceOrientation == UIInterfaceOrientationPortrait);

}

39Friday, January 22, 2010

Page 80: 06 View Controllers

Supporting Interface Rotation

40Friday, January 22, 2010

Page 81: 06 View Controllers

Supporting Interface Rotation- (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation{

// This view controller supports all orientations// except for upside-down.return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

40Friday, January 22, 2010

Page 82: 06 View Controllers

Demo:Rotating Your Interface

41Friday, January 22, 2010

Page 83: 06 View Controllers

Autoresizing Your Views

42Friday, January 22, 2010

Page 84: 06 View Controllers

Autoresizing Your Viewsview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

42Friday, January 22, 2010

Page 85: 06 View Controllers

Autoresizing Your Viewsview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

42Friday, January 22, 2010

Page 86: 06 View Controllers

Autoresizing Your Viewsview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

42Friday, January 22, 2010

Page 87: 06 View Controllers

Autoresizing Your Viewsview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

42Friday, January 22, 2010

Page 88: 06 View Controllers

Questions?

43Friday, January 22, 2010