You are on page 1of 32

Stanford CS193p

Developing Applications for iOS


Winter 2015

CS193p!
Winter 2015
Today
Continuation of Calculator Demo
Another thousand words (or so)?

MVC
Object-Oriented Design Pattern

CS193p!
Winter 2015
Demo
Calculator continued
Array<T>
Computed properties (instance variables which are computed rather than stored)
switch
Functions as types
Closure syntax for defining functions on the fly
Methods with the same name but different argument types
More Autolayout

CS193p!
Winter 2015
MVC

Controller

Model View

Divide objects in your program into 3 camps.


CS193p!
Winter 2015
MVC

Controller

Model View

Model = What your application is (but not how it is displayed)


CS193p!
Winter 2015
MVC

Controller

Model View

Controller = How your Model is presented to the user (UI logic)


CS193p!
Winter 2015
MVC

Controller

Model View

View = Your Controllers minions


CS193p!
Winter 2015
MVC

Controller

Model View

Its all about managing communication between camps


CS193p!
Winter 2015
MVC

Controller

Model View

Controllers can always talk directly to their Model.


CS193p!
Winter 2015
MVC

Controller
outlet

Model View

Controllers can also talk directly to their View.


CS193p!
Winter 2015
MVC

Controller
outlet

Model View

The Model and View should never speak to each other.


CS193p!
Winter 2015
MVC

Controller
?
outlet

Model View

Can the View speak to its Controller?


CS193p!
Winter 2015
MVC

Controller
outlet

Model View

Sort of. Communication is blind and structured.


CS193p!
Winter 2015
MVC
target

Controller
outlet

Model View

The Controller can drop a target on itself.


CS193p!
Winter 2015
MVC
target

Controller
outlet

action

Model View

Then hand out an action to the View.


CS193p!
Winter 2015
MVC
target

Controller
outlet

action

Model View

The View sends the action when things happen in the UI.
CS193p!
Winter 2015
MVC
target

Controller
outlet

action
should

will did

Model View

Sometimes the View needs to synchronize with the Controller.


CS193p!
Winter 2015
MVC
should target
will did

Controller
outlet

de
le
ga
action

te
Model View

The Controller sets itself as the Views delegate.


CS193p!
Winter 2015
MVC
should target
will did

Controller
outlet

de
le
ga
action

te
Model View

The delegate is set via a protocol (i.e. its blind to class).


CS193p!
Winter 2015
MVC
should target
will did

Controller
outlet

de
le
ga
action

te
Model View

Views do not own the data they display.


CS193p!
Winter 2015
MVC
should target
will did

Controller
outlet

de
le
ga
action

te
Model data View
count
at

So, if needed, they have a protocol to acquire it.


CS193p!
Winter 2015
MVC
should target
will did

Controller
data
count
outlet
at

da

de
ta

le
ga
so action

te
u rc
e

Model View

Controllers are almost always that data source (not Model!).


CS193p!
Winter 2015
MVC
should target
will did

Controller
data
count
outlet
at

da

de
ta

le
ga
so action

te
u rc
e

Model View

Controllers interpret/format Model information for the View.


CS193p!
Winter 2015
MVC
should target
will did

Controller
data outlet

?
count
at

da

de
ta

le
ga
so action

te
u rc
e

Model View

Can the Model talk directly to the Controller?


CS193p!
Winter 2015
MVC
should target
will did

Controller
data
count
outlet
at

da

de
ta

le
ga
so action

te
u rc
e

Model View

No. The Model is (should be) UI independent.


CS193p!
Winter 2015
MVC
should target
will did

Controller
data
count
outlet
at

da

de
ta

le
ga
so action

te
u rc
e

Model View

So what if the Model has information to update or something?


CS193p!
Winter 2015
MVC
should target
will did

Controller
data
count
outlet
at

da

de
Notification!
ta

le
& KVO

ga
so action

te
u rc
e

Model View

It uses a radio station-like broadcast mechanism.


CS193p!
Winter 2015
MVC
should target
will did

Controller
data
count
outlet
at

da

de
Notification!
ta

le
& KVO

ga
so action

te
u rc
e

Model View

Controllers (or other Model) tune in to interesting stuff.


CS193p!
Winter 2015
MVC
should target
will did

Controller
data
count
outlet
at

da

de
Notification!
ta

le
& KVO

ga
so action

te
u rc
e

Model View

A View might tune in, but probably not to a Models station.


CS193p!
Winter 2015
MVC
should target
will did

Controller
data
count
outlet
at

da

de
Notification!
ta

le
& KVO

ga
so action

te
u rc
e

Model View

Now combine MVC groups to make complicated programs ...


CS193p!
Winter 2015
MVCs working together

CS193p!
Winter 2015
MVCs not working together

CS193p!
Winter 2015

You might also like