standford cs 193p: 04-model view controller

Upload: oleksiy-kovyrin

Post on 30-May-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    1/56

    CS193P - Lecture 4

    iPhone Application Development

    Application LifecycleModel, View, ControllerNib FilesControls and Target-Action

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    2/56

    Announcements

    No more NDA Yay!

    Class website and materials will remain publicly accessible

    Still planning to set up university team

    Enables on-device development Enables students to build apps and distribute them

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    3/56

    Todays Topics

    HelloPoly demo

    Application Lifecycle

    Model, View, Controller design

    Interface Builder and Nib Files

    Controls and Target-Action

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    4/56

    HelloPoly Demo

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    5/56

    Application Lifecycle

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    6/56

    What makes an app tick?

    Anatomy of an application Compiled code

    Your code

    Cocoa Touch framework code

    Nib files Contains user interface elements (along with other objects)

    Includes details about object relationships

    Resources (sounds, images, etc)

    Info.plist file (application configuration)

    How do all the pieces fit together?

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    7/56

    App Lifecycle

    Launcha

    pp

    Loadm

    ainnib

    Waitf

    orevent

    Hand

    leevent

    Exitapp

    Appin

    itialized

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    8/56

    UIKit Framework

    UIKit drives the show, youre just along for the ride

    Every application has a single instance of UIApplication Singleton design pattern

    @interface UIApplication

    + (UIApplication *)sharedApplication@end

    Orchestrates the lifecycle of an application

    Dispatches events

    Manages status bar, application icon badge

    Rarely subclassed

    Uses delegation instead

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    9/56

    Delegation

    Control passed to delegate objects to perform application-specific behavior

    Avoids need to subclass complex objects

    Many UIKit classes use delegates

    UIApplication UITableView

    UITextField

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    10/56

    Xcode project templates have one set up by default

    Object you provide that participates in application lifecycle

    Can implement various methods which UIApplication will call

    Examples:

    UIApplicationDelegate

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    11/56

    Xcode project templates have one set up by default

    Object you provide that participates in application lifecycle

    Can implement various methods which UIApplication will call

    Examples:

    - (void)applicationDidFinishLaunching:(UIApplication *)application;

    - (void)applicationWillTerminate:(UIApplication *)application;

    UIApplicationDelegate

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    12/56

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    13/56

    Xcode project templates have one set up by default

    Object you provide that participates in application lifecycle

    Can implement various methods which UIApplication will call

    Examples:

    - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;

    - (void)applicationWillResignActive:(UIApplication *)application;

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;

    - (void)applicationDidFinishLaunching:(UIApplication *)application;

    - (void)applicationWillTerminate:(UIApplication *)application;

    UIApplicationDelegate

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    14/56

    Info.plist file

    A property list containing various pieces of application-specificinformation Status bar style (default, black, hidden)

    If your application is a landscape application

    Whether app icon has prerendered shine and bevel Whether your app uses wifi for communication

    If your application is iPhone only (non-iPod touch)

    Application icon

    Can edit most pieces of information using interface in Xcode Project > Edit Active Target Foo menu item

    On the properties tab

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    15/56

    Working with the frameworks

    UIKit provides lots of functionality

    UIKit and you Dont fight the frameworks

    Understand the designs and how you fit into them

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    16/56

    Model, View, Controller

    If you take nothing else away from this class...

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    17/56

    HelloPoly

    This weeks assignment is a full MVC application

    Next weeks assignment will flesh it out further

    It is not designed to be a complex application rather, provide a series of small studies of the fundamentals of a

    Cocoa Touch application

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    18/56

    Model, View, Controller

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    19/56

    Model, View, Controller

    Divides an application into 3 main functional pieces

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    20/56

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    21/56

    Model, View, Controller

    Divides an application into 3 main functional pieces

    Model Manages the app data and state, not concerned with UI or

    presentation

    Often persists somewhere

    View Displays the model objects to the user

    Allows user to manipulate data by responding to events

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    22/56

    Model, View, Controller

    Divides an application into 3 main functional pieces

    Model Manages the app data and state, not concerned with UI or

    presentation

    Often persists somewhere

    View Displays the model objects to the user

    Allows user to manipulate data by responding to events

    Controller Coordinates the model and the view, keeps the view updated

    when model changes, etc. Typically where app logic is.

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    23/56

    Model, View, Controller

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    24/56

    Model, View, Controller

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    25/56

    Model, View, Controller

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    26/56

    Model, View, Controller

    HelloPoly

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    27/56

    Model, View, Controller

    PolygonShape

    HelloPoly

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    28/56

    Model, View, Controller

    PolygonShape UIKit controlsPolygonView (next week)

    HelloPoly

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    29/56

    Model, View, Controller

    PolygonShape

    Controller

    UIKit controlsPolygonView (next week)

    HelloPoly

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    30/56

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    31/56

    increaseButton

    numberOfSidesLabel

    Model, View, Controller

    HelloPoly

    PolygonShape

    Controller

    decreaseButton

    increase

    decrease

    polygonShape

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    32/56

    increaseButton

    numberOfSidesLabel

    Model, View, Controller

    HelloPoly

    PolygonShape

    Controller

    decreaseButton

    increase

    decrease

    polygonShape

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    33/56

    increaseButton

    numberOfSidesLabel

    Model, View, Controller

    HelloPoly

    PolygonShape

    Controller

    decreaseButton

    increase

    decrease

    polygonShape

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    34/56

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    35/56

    Model, View, Controller

    HelloPoly

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    36/56

    Interface Builder and Nib Files

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    37/56

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    38/56

    Nib Files - Design time

    At design time in IB, you layout user interface elements

    add controller objects

    define connections between controller and UI

    When saved, objects are archived Standardized archiving / serialization mechanism

    NSCoding

    Archives and Serialization Programming Guide

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    39/56

    Nib Loading

    At runtime, objects are unarchived Objects have same values/settings as in Interface Builder

    Ensures all outlets and actions are connected

    Order of unarchiving is not defined

    If loading the nib automatically creates objects and order isundefined, how do I customize? For example, restore previous polygon information

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    40/56

    -awakeFromNib

    Control point to implement any additional logic after nibloading

    You often implement it in your controller class e.g. to restore previously saved application state

    Guaranteed everything has been unarchived from nib, and allconnections are made before -awakeFromNib is called

    - (void)awakeFromNib {

    // do customization here

    }

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    41/56

    Controls and Target-Action

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    42/56

    increaseButton

    numberOfSidesLabel

    Model, View, Controller

    HelloPolyController

    decreaseButton

    increase

    decrease

    polygonShape

    PolygonShape

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    43/56

    increaseButton

    numberOfSidesLabel

    Model, View, Controller

    HelloPolyController

    decreaseButton

    increase

    decrease

    polygonShape

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    44/56

    Controls - Events

    View objects that allows users to initiate some type of action Respond to variety of events

    Touch events

    touchDown

    touchDragged (entered, exited, drag inside, drag outside) touchUp (inside, outside)

    Value changed

    Editing events

    editing began

    editing changed

    editing ended

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    45/56

    Controls - Target/Action

    When event occurs, action is invoked on target object

    Controller

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    46/56

    Controls - Target/Action

    When event occurs, action is invoked on target object

    target: myObject

    action: @selector(decrease)

    event: UIControlEventTouchUpInside

    Controller

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    47/56

    Controls - Target/Action

    When event occurs, action is invoked on target object

    target: myObject

    action: @selector(decrease)

    event: UIControlEventTouchUpInside

    Controller

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    48/56

    Controls - Target/Action

    When event occurs, action is invoked on target object

    target: myObject

    action: @selector(decrease)

    event: UIControlEventTouchUpInside

    Controller

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    49/56

    Controls - Target/Action

    When event occurs, action is invoked on target object

    UIControlEventTouchUpInside

    target: myObject

    action: @selector(decrease)

    event: UIControlEventTouchUpInside

    Controller

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    50/56

    Controls - Target/Action

    When event occurs, action is invoked on target object

    UIControlEventTouchUpInside

    -(void)decrease

    target: myObject

    action: @selector(decrease)

    event: UIControlEventTouchUpInside

    Controller

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    51/56

    Action Methods

    3 different flavors of action method selector types- (void)actionMethod;

    - (void)actionMethod:(id)sender;

    - (void)actionMethod:(id)sender withEvent:(UIEvent *)event;

    UIEvent contains details about the event that took place

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    52/56

    Action Method Variations

    - (void)increase {

    // bump the number of sides of the polygon up

    polygon.numberOfSides += 1;

    }

    // for example, if control is a slider...

    - (void)adjustNumberOfSides:(id)sender {

    polygon.numberOfSides = [sender value];}

    Simple no-argument selector

    Single argument selector - control is sender

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    53/56

    Action Method Variations

    - (void)adjustNumberOfSides:(id)sender

    withEvent:(UIEvent *)event

    {

    // could inspect event object if you needed to

    }

    Simple no-argument selector

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    54/56

    Multiple target-actions

    Controls can trigger multiple actions on different targets inresponse to the same event Different than Cocoa on the desktop where only one target-

    action is supported

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    55/56

    Manual Target-Action

    @interface UIControl

    - (void)addTarget:(id)target action:(SEL)actionforControlEvents:(UIControlEvents)controlEvents;

    - (void)removeTarget:(id)target action:(SEL)action

    forControlEvents:(UIControlEvents)controlEvents;

    @end

    Same information IB would use API and UIControlEvents found in UIControl.h

    UIControlEvents is a bitmask

  • 8/14/2019 Standford CS 193P: 04-Model View Controller

    56/56

    Questions?