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

Model

outlet

View

Controllers can also talk directly to their View.


CS193p!
Winter 2015

MVC
Controller

Model

outlet

View

The Model and View should never speak to each other.


CS193p!
Winter 2015

MVC
Controller

Model

outlet

?
View

Can the View speak to its Controller?


CS193p!
Winter 2015

MVC
Controller

Model

outlet

View

Sort of. Communication is blind and structured.


CS193p!
Winter 2015

MVC
target

Controller

Model

outlet

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

Model

will

did

View

Sometimes the View needs to synchronize with the Controller.


CS193p!
Winter 2015

MVC
target

should
will

did

Controller

outlet
te
ga
le
de

Model

action

View

The Controller sets itself as the Views delegate.


CS193p!
Winter 2015

MVC
target

should
will

did

Controller

outlet
te
ga
le
de

Model

action

View

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


CS193p!
Winter 2015

MVC
target

should
will

did

Controller

outlet
te
ga
le
de

Model

action

View

Views do not own the data they display.


CS193p!
Winter 2015

MVC
target

should
will

did

Controller

outlet
te
ga
le
de

Model

data
at

action

View
count

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


CS193p!
Winter 2015

MVC
target

should
will

did

Controller
data
at

count

Model

te
ga
le
de

da
ta

outlet

so
u

rc
e

action

View

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


CS193p!
Winter 2015

MVC
target

should
will

did

Controller
data
at

count

Model

te
ga
le
de

da
ta

outlet

so
u

rc
e

action

View

Controllers interpret/format Model information for the View.


CS193p!
Winter 2015

MVC
target

should
will

did

Controller

Model

count

da
ta

outlet
te
ga
le
de

data
at

so
u

rc
e

action

View

Can the Model talk directly to the Controller?


CS193p!
Winter 2015

MVC
target

should
will

did

Controller
data
at

count

Model

te
ga
le
de

da
ta

outlet

so
u

rc
e

action

View

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


CS193p!
Winter 2015

MVC
target

should
will

did

Controller
data
at

count

Model

te
ga
le
de

da
ta

outlet

so
u

rc
e

action

View

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


CS193p!
Winter 2015

MVC
target

should
will

did

Controller
data
at

Model

da
ta

outlet
te
ga
le
de

Notification!
& KVO

count

so
u

rc
e

action

View

It uses a radio station-like broadcast mechanism.


CS193p!
Winter 2015

MVC
target

should
will

did

Controller
data
at

Model

da
ta

outlet
te
ga
le
de

Notification!
& KVO

count

so
u

rc
e

action

View

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


CS193p!
Winter 2015

MVC
target

should
will

did

Controller
data
at

Model

da
ta

outlet
te
ga
le
de

Notification!
& KVO

count

so
u

rc
e

action

View

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


CS193p!
Winter 2015

MVC
target

should
will

did

Controller
data
at

Model

da
ta

outlet
te
ga
le
de

Notification!
& KVO

count

so
u

rc
e

action

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