0% found this document useful (0 votes)
400 views42 pages

Spring 2010: Wednesday, March 31, 2010

The document provides an overview of topics covered in the CS193p Spring 2010 class, including the Model-View-Controller (MVC) pattern, Objective-C, Interface Builder, and Xcode. It also discusses implementing a simple calculator application using these concepts, with code snippets showing how to define a CalculatorBrain model class interface with methods for setting operands and performing operations.

Uploaded by

xuan12k
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
400 views42 pages

Spring 2010: Wednesday, March 31, 2010

The document provides an overview of topics covered in the CS193p Spring 2010 class, including the Model-View-Controller (MVC) pattern, Objective-C, Interface Builder, and Xcode. It also discusses implementing a simple calculator application using these concepts, with code snippets showing how to define a CalculatorBrain model class interface with methods for setting operands and performing operations.

Uploaded by

xuan12k
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

CS193p

Spring 2010

Wednesday, March 31, 2010


Enrollment Closed
You should have received an e-mail
It will confirm your grading status (P/NC or not)
As usual, we were oversubscribed for grading option
Sorry to anyone who didn’t get the option they wanted
If you received e-mail, but are not in Axess, do it!

... and an invitation to iPhone Developer Program

If not, e-mail [email protected].

Wednesday, March 31, 2010


Communication

E-mail
Questions are best sent to [email protected]
Sending directly to instructor or TA’s risks slow response.

Web Site
Very Important!
http://cs193p.stanford.edu
All lectures, assignments, code, etc. will be there.
This site will be your best friend when it comes to getting info.

Wednesday, March 31, 2010


Office Hours
Andreas
Monday 6pm to 8pm
Thursday 6pm to 8pm
Gates B26A
Bring your Stanford ID card for access to the building

Sonali
Friday 11am to 1pm
Thursday 1pm to 3pm
Gates B26B

Wednesday, March 31, 2010


Today’s Topics
MVC
Calculator

Objective-C
Declaring and implementing objects
Sending messages between objects

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

Xcode
Managing all your code
Running your application in the simulator

Wednesday, March 31, 2010


Our Calculator
CalculatorViewController

Controller

UILabel

Model View
UIButton UIButton
CalculatorBrain UIButton UIButton
UIButton UIButton UIButton

Wednesday, March 31, 2010


Header File (public API)

Model

@interface

@end

Wednesday, March 31, 2010


Header File (public API)

Model

@interface CalculatorBrain

@end

Wednesday, March 31, 2010


Header File (public API)

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

@end

Wednesday, March 31, 2010


Header File (public API)

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

@end

Wednesday, March 31, 2010


Header File (public API)

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

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

@end

Wednesday, March 31, 2010


Header File (public API)

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

Wednesday, March 31, 2010


Header File (public API)

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

Wednesday, March 31, 2010


Header File (public API)

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

Wednesday, March 31, 2010


Header File (public API)

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

Wednesday, March 31, 2010


Header File (public API)

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

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

This method returns a double.

@end

Wednesday, March 31, 2010


Header File (public API)

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

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

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


That’s right, we’re passing an object to this method.
@end

Wednesday, March 31, 2010


Header File (public API)

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

Wednesday, March 31, 2010


Header File (public API)

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 This method takes two arguments and is called “foo:bar:”

Wednesday, March 31, 2010


Header File (public API)

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
(a collection class in Foundation).

Wednesday, March 31, 2010


Header File (public API)

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”


This means “a pointer to *ANY* kind of object!”

Wednesday, March 31, 2010


Header File (public API)

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

Wednesday, March 31, 2010


Header File (public API)

Model
#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject


{
double operand;
}

- (void)setOperand:(double)anOperand;

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

@end

Wednesday, March 31, 2010


Implementation File
(private and public)

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end

Wednesday, March 31, 2010


Implementation File
(private and public)

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

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

- (double)performOperation:(NSString *)operation
{
<code goes here>
return aDouble;
}
@end

Wednesday, March 31, 2010


Implementation File
(private and public)

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain No semicolon this time!

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

- (double)performOperation:(NSString *)operation
{
<code goes here>
return aDouble;
}
@end

Wednesday, March 31, 2010


Implementation File
(private and public)

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

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

- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
}
@end

Wednesday, March 31, 2010


Implementation File
(private and public)

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

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

- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
} Square brackets mean “send a message.”
@end

Wednesday, March 31, 2010


Implementation File
(private and public)

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

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

- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
}
@end This is the object to send the message to
(in this case, the NSString called “operation” that was
passed as an argument to performOperation:).

Wednesday, March 31, 2010


Implementation File
(private and public)

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

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

- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
} This is the message to send.
@end

Wednesday, March 31, 2010


Implementation File
(private and public)

Model
#import “CalculatorBrain.h”

@implementation CalculatorBrain

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

- (double)performOperation:(NSString *)operation
{
[operation sendMessage:argument];
return aDouble;
} And this is its one (in this case) argument.
@end

Wednesday, March 31, 2010


Controller

#import <UIKit/UIKit.h>

@interface CalculatorViewController : UIViewController


{
CalculatorBrain * brain;
IBOutlet UILabel * display;
}

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;

@end

Wednesday, March 31, 2010


Controller
Our Controller inherits from
UIViewController. UIKit
#import <UIKit/UIKit.h> supports MVC primarily
through this class.

@interface CalculatorViewController : UIViewController


{
CalculatorBrain * brain;
IBOutlet UILabel * display;
}

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;

@end

Wednesday, March 31, 2010


Controller

#import <UIKit/UIKit.h>

@interface CalculatorViewController : UIViewController


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

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;

@end

Wednesday, March 31, 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;

@end

Wednesday, March 31, 2010


View

Controller

#import <UIKit/UIKit.h>

@interface CalculatorViewController : UIViewController


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

- (IBAction)digitPressed:(UIButton *)sender;
- (IBAction)operationPressed:(UIButton *)sender;

@end

Wednesday, March 31, 2010


CalculatorViewController.xib

Wednesday, March 31, 2010


“File’s Owner” is our
Controller

CalculatorViewController.xib

Wednesday, March 31, 2010


Wednesday, March 31, 2010
Wednesday, March 31, 2010
Wednesday, March 31, 2010
Xcode

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

Wednesday, March 31, 2010

You might also like