Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing...

32
CS193p Winter 201 5 Stanford CS193p Developing Applications for iOS Winter 2015

Transcript of Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing...

Page 1: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Stanford CS193pDeveloping Applications for iOS

Winter 2015

Page 2: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

TodayContinuation of Calculator Demo

Another thousand words (or so)?

MVCObject-Oriented Design Pattern

Page 3: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

DemoCalculator continued …Array<T>“Computed” properties (instance variables which are computed rather than stored)switchFunctions as typesClosure syntax for defining functions “on the fly”Methods with the same name but different argument typesMore Autolayout

Page 4: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

Divide objects in your program into 3 “camps.”

Page 5: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

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

Page 6: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

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

Page 7: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

View = Your Controller’s minions

Page 8: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

It’s all about managing communication between camps

Page 9: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

Controllers can always talk directly to their Model.

Page 10: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

outlet

Controllers can also talk directly to their View.

Page 11: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

outlet

The Model and View should never speak to each other.

Page 12: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

?outlet

Can the View speak to its Controller?

Page 13: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

outlet

Sort of. Communication is “blind” and structured.

Page 14: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

outlet

target

The Controller can drop a target on itself.

Page 15: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

outlet

target

Then hand out an action to the View.

Page 16: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

outlet

target

The View sends the action when things happen in the UI.

Page 17: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

outlet

should

will did

target

Sometimes the View needs to synchronize with the Controller.

Page 18: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

delegate

outlet

should

will did

target

The Controller sets itself as the View’s delegate.

Page 19: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

delegate

outlet

should

will did

target

The delegate is set via a protocol (i.e. it’s “blind” to class).

Page 20: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

delegate

outlet

should

will did

target

Views do not own the data they display.

Page 21: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

delegate

outlet

should

will did

target

countdata at

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

Page 22: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

delegate

outlet

data source

should

will did

target

countdata at

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

Page 23: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controllers interpret/format Model information for the View.

Controller

MVC

Model View

action

delegate

outlet

data source

should

will did

target

countdata at

Page 24: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

? delegate

outlet

data source

should

will did

target

countdata at

Can the Model talk directly to the Controller?

Page 25: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

delegate

outlet

data source

should

will did

target

countdata at

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

Page 26: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

delegate

outlet

data source

should

will did

target

countdata at

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

Page 27: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

Notification!& KVO

delegate

outlet

data source

should

will did

target

countdata at

It uses a “radio station”-like broadcast mechanism.

Page 28: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

Notification!& KVO

delegate

outlet

data source

should

will did

target

countdata at

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

Page 29: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

A View might “tune in,” but probably not to a Model’s “station.”

Controller

MVC

Model View

action

Notification!& KVO

delegate

outlet

data source

should

will did

target

countdata at

Page 30: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

Controller

MVC

Model View

action

Notification!& KVO

delegate

outlet

data source

should

will did

target

countdata at

Now combine MVC groups to make complicated programs ...

Page 31: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

MVCs working together

Page 32: Stanford CS193p - theopenacademy.com Science... · CS193p! Winter 2015 Stanford CS193p Developing Applications for iOS Winter 2015

CS193p!Winter 2015

MVCs not working together