0% found this document useful (0 votes)
248 views

Stanford Cs193P: Developing Applications For Iphone 4, Ipod Touch, & Ipad Fall 2010

This document summarizes the Stanford CS193p course on developing iPhone applications using Objective-C and the Model-View-Controller (MVC) pattern. It provides an overview of the key topics covered in the course, including Objective-C, Interface Builder, Xcode, and building a sample calculator application using MVC. The document contains code snippets and diagrams showing how to implement the model, view, and controller components of the calculator app.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
248 views

Stanford Cs193P: Developing Applications For Iphone 4, Ipod Touch, & Ipad Fall 2010

This document summarizes the Stanford CS193p course on developing iPhone applications using Objective-C and the Model-View-Controller (MVC) pattern. It provides an overview of the key topics covered in the course, including Objective-C, Interface Builder, Xcode, and building a sample calculator application using MVC. The document contains code snippets and diagrams showing how to implement the model, view, and controller components of the calculator app.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Stanford CS193p

Developing Applications for iPhone 4, iPod Touch, & iPad


Fall 2010

Stanford
CS193p
Fall 2010
Today
MVC
Calculator

Objective-C
Declaring and implementing objects
Sending messages between objects

Interface Builder
Graphically creating your View
“Wiring up” objects to send messages to each other
Setting the properties of objects

Xcode
Managing and editing your code
Running your application in the simulator
Stanford
CS193p
Fall 2010
Calculator MVC
should target
will did

Controller
data
count
at
outlet

de
da
ta

le
g
so

at
e
u rc action
e

Model View
Stanford
CS193p
Fall 2010
Calculator MVC
target

Controller

outlet

action

Model View
Stanford
CS193p
Fall 2010
Calculator MVC
target

Controller

outlet

action

Model View

CalculatorBrain Stanford
CS193p
Fall 2010
Calculator MVC
CalculatorViewController
target

Controller

outlet

action

Model View

CalculatorBrain Stanford
CS193p
Fall 2010
Calculator MVC
CalculatorViewController
target

Controller

outlet

el
b
La
action

UI
3.21 1
7 X

Model 4 View -
+ 2

UIButtons
CalculatorBrain Stanford
CS193p
Fall 2010
Calculator MVC
CalculatorViewController
target

Controller

display

el
b
La
action

UI
3.21 1
7 X

Model 4 View -
+ 2

UIButtons
CalculatorBrain Stanford
CS193p
Fall 2010
Calculator MVC
CalculatorViewController
target

Controller

display

el
b
La
operationPressed:

UI
digitPressed:
3.21 1
7 X

Model 4 View -
+ 2

UIButtons
CalculatorBrain Stanford
CS193p
Fall 2010
CalculatorBrain.h This is the header file for this class.
It documents its public API.
Model

Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


The name of this class.

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


This class’s superclass.

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

We must import the header for our superclass.


Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
Instance variables go here.
}

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

Method
declarations
go here.

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}
Specifying void as the return type means
that this method returns no value.

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}
The name of this method is “setOperand:”

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}
It takes one argument, a double called “anOperand”

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}
Don’t forget a semicolon here!

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

This method returns a double.

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

It takes as its argument a pointer to an NSString object.


That’s right, we’re passing an object to this method.
@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

- (NSArray *)foo:(int)zap bar:(id)pow;

@end Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

- (NSArray *)foo:(int)zap bar:(id)pow;

This method takes two arguments and is called “foo:bar:”


@end (pronounced “foo colon bar colon”) Stanford
CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

- (NSArray *)foo:(int)zap bar:(id)pow;

It returns a pointer to an NSArray


@end Stanford
(a collection class in Foundation). CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

- (NSArray *)foo:(int)zap bar:(id)pow;

@end The second argument is of type “id”. Stanford


This means “a pointer to any kind of object!” CS193p
Fall 2010
CalculatorBrain.h

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

- (double)performOperation:(NSString *)operation;

@end Stanford
CS193p
Fall 2010
CalculatorBrain.m This is the implementation file.
Both public and private implementation
goes here. Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end
Stanford
CS193p
Fall 2010
CalculatorBrain.m

We must import our own header file.


Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end
Stanford
CS193p
Fall 2010
CalculatorBrain.m

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

Note that we don’t specify our superclass in the implementation

@end
Stanford
CS193p
Fall 2010
CalculatorBrain.m

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain No semicolon this time!

- (void)setOperand:(double)anOperand
{
<code goes here>
}

@end
Stanford
CS193p
Fall 2010
CalculatorBrain.m

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

- (void)setOperand:(double)anOperand
{
operand = anOperand;
}
- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
}
@end
Stanford
CS193p
Fall 2010
CalculatorBrain.m

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

- (void)setOperand:(double)anOperand
{
operand = anOperand;
}
- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
} Square brackets mean “send a message.”
@end
Stanford
CS193p
Fall 2010
CalculatorBrain.m

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

- (void)setOperand:(double)anOperand
{
operand = anOperand;
}
- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
}
@end This is the object to send the message to
Stanford
(in this case, the NSString called “operation” that was
CS193p
passed as an argument to performOperation:). Fall 2010
CalculatorBrain.m

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

- (void)setOperand:(double)anOperand
{
operand = anOperand;
}
- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
} This is the message to send.

@end
Stanford
CS193p
Fall 2010
CalculatorBrain.m

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

- (void)setOperand:(double)anOperand
{
operand = anOperand;
}
- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
} And this is its one (in this case) argument.

@end
Stanford
CS193p
Fall 2010
CalculatorBrain.m

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

- (void)setOperand:(double)anOperand
{
operand = anOperand;
}
- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
}
@end
Stanford
CS193p
Fall 2010
Controller

#import <UIKit/UIKit.h>

@interface CalculatorViewController : UIViewController


{
CalculatorBrain * brain;
IBOutlet UILabel * display;
}

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;
Stanford
@end CS193p
Fall 2010
Controller
Our Controller inherits from
UIViewController. UIKit
supports MVC primarily
#import <UIKit/UIKit.h> through this class.

@interface CalculatorViewController : UIViewController


{
CalculatorBrain * brain;
IBOutlet UILabel * display;
}

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;
Stanford
@end CS193p
Fall 2010
Controller

#import <UIKit/UIKit.h>

@interface CalculatorViewController : UIViewController


{
This is going to point to our Model
CalculatorBrain * brain;
CalculatorBrain
IBOutlet UILabel * display;
}

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;
Stanford
@end CS193p
Fall 2010
These hook up to our View

Controller

#import <UIKit/UIKit.h>

@interface CalculatorViewController : UIViewController


{
CalculatorBrain * brain;
IBOutlet UILabel * display;
}

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;
Stanford
@end CS193p
Fall 2010
View

Controller

#import <UIKit/UIKit.h>

@interface CalculatorViewController : UIViewController


{ Model
CalculatorBrain * brain;
IBOutlet UILabel * display;
}

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;
Stanford
@end CS193p
Fall 2010
Stanford
CS193p
CalculatorViewController.xib
Fall 2010
“File’s Owner” is our
Controller Stanford
CS193p
CalculatorViewController.xib
Fall 2010
Stanford
CS193p
Fall 2010
Stanford
CS193p
Fall 2010
Stanford
CS193p
Fall 2010
My First Project

A picture (or demo) is worth 1,000 words.

Stanford
CS193p
Fall 2010

You might also like