Stanford CS193p Developing Applications for iOS Winter 2017 CS193p - - PowerPoint PPT Presentation

stanford cs193p
SMART_READER_LITE
LIVE PREVIEW

Stanford CS193p Developing Applications for iOS Winter 2017 CS193p - - PowerPoint PPT Presentation

Stanford CS193p Developing Applications for iOS Winter 2017 CS193p Winter 2017 Today MVC Object-Oriented Design Pattern Continuation of Calculator Demo Computed Properties, MVC, Laying out the UI to work with different devices CS193p Winter


slide-1
SLIDE 1

CS193p Winter 2017

Stanford CS193p

Developing Applications for iOS Winter 2017

slide-2
SLIDE 2

CS193p Winter 2017

Today

MVC

Object-Oriented Design Pattern

Continuation of Calculator Demo

Computed Properties, MVC, Laying out the UI to work with different devices

slide-3
SLIDE 3

CS193p Winter 2017

Controller

MVC

Model View

Divide objects in your program into 3 “camps. ”

slide-4
SLIDE 4

CS193p Winter 2017

Controller

MVC

Model View

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

slide-5
SLIDE 5

CS193p Winter 2017

Controller

MVC

Model View

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

slide-6
SLIDE 6

CS193p Winter 2017

Controller

MVC

Model View

View = Your Controller’ s minions

slide-7
SLIDE 7

CS193p Winter 2017

Controller

MVC

Model View

It’ s all about managing communication between camps

slide-8
SLIDE 8

CS193p Winter 2017

Controller

MVC

Model View

Controllers can always talk directly to their Model.

slide-9
SLIDE 9

CS193p Winter 2017

Controller

MVC

Model View

  • utlet

Controllers can also talk directly to their View.

slide-10
SLIDE 10

CS193p Winter 2017

Controller

MVC

Model View

  • utlet

The Model and View should never speak to each other.

slide-11
SLIDE 11

CS193p Winter 2017

Controller

MVC

Model View

?

  • utlet

Can the View speak to its Controller?

slide-12
SLIDE 12

CS193p Winter 2017

Controller

MVC

Model View

  • utlet

Sort of. Communication is “blind” and structured.

slide-13
SLIDE 13

CS193p Winter 2017

Controller

MVC

Model View

  • utlet

target

The Controller can drop a target on itself.

slide-14
SLIDE 14

CS193p Winter 2017

Controller

MVC

Model View

action

  • utlet

target

Then hand out an action to the View.

slide-15
SLIDE 15

CS193p Winter 2017

Controller

MVC

Model View

action

  • utlet

target

Then hand out an action to the View.

slide-16
SLIDE 16

CS193p Winter 2017

Controller

MVC

Model View

action

  • utlet

target

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

slide-17
SLIDE 17

CS193p Winter 2017

Controller

MVC

Model View

action

  • utlet

should will did

target

Sometimes the View needs to synchronize with the Controller.

slide-18
SLIDE 18

CS193p Winter 2017

Controller

MVC

Model View

action delegate

  • utlet

should will did

target

The Controller sets itself as the View’ s delegate.

slide-19
SLIDE 19

CS193p Winter 2017

Controller

MVC

Model View

action delegate

  • utlet

should will did

target

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

slide-20
SLIDE 20

CS193p Winter 2017

Controller

MVC

Model View

action delegate

  • utlet

should will did

target

Views do not own the data they display.

slide-21
SLIDE 21

CS193p Winter 2017

Controller

MVC

Model View

action delegate

  • utlet

should will did

target

count data at

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

slide-22
SLIDE 22

CS193p Winter 2017

Controller

MVC

Model View

action delegate

  • utlet

d a t a s

  • u

r c e

should will did

target

count data at

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

slide-23
SLIDE 23

CS193p Winter 2017

Controllers interpret/format Model information for the View.

Controller

MVC

Model View

action delegate

  • utlet

d a t a s

  • u

r c e

should will did

target

count data at

slide-24
SLIDE 24

CS193p Winter 2017

Controller

MVC

Model View

action

?

delegate

  • utlet

d a t a s

  • u

r c e

should will did

target

count data at

Can the Model talk directly to the Controller?

slide-25
SLIDE 25

CS193p Winter 2017

Controller

MVC

Model View

action delegate

  • utlet

d a t a s

  • u

r c e

should will did

target

count data at

  • No. The Model is (should be) UI independent.
slide-26
SLIDE 26

CS193p Winter 2017

Controller

MVC

Model View

action delegate

  • utlet

d a t a s

  • u

r c e

should will did

target

count data at

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

slide-27
SLIDE 27

CS193p Winter 2017

Controller

MVC

Model View

action Notification & KVO delegate

  • utlet

d a t a s

  • u

r c e

should will did

target

count data at

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

slide-28
SLIDE 28

CS193p Winter 2017

Controller

MVC

Model View

action Notification & KVO delegate

  • utlet

d a t a s

  • u

r c e

should will did

target

count data at

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

slide-29
SLIDE 29

CS193p Winter 2017

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

Controller

MVC

Model View

action Notification & KVO delegate

  • utlet

d a t a s

  • u

r c e

should will did

target

count data at

slide-30
SLIDE 30

CS193p Winter 2017

Controller

MVC

Model View

action Notification & KVO delegate

  • utlet

d a t a s

  • u

r c e

should will did

target

count data at

Now combine MVC groups to make complicated programs ...

slide-31
SLIDE 31

CS193p Winter 2017

MVCs working together

slide-32
SLIDE 32

CS193p Winter 2017

MVCs not working together

slide-33
SLIDE 33

CS193p Winter 2017

Demo

Calculator continued …

MVC

struct vs. class (mutating, etc.)

public versus private API more examples of Optional

Dictionary<KeyType,ValueType> enum

associated values

switch

Functions as types Closure syntax for defining functions “on the fly”

UIStackView

First peek at Autolayout (stick things to the edges)