Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

42
CS193p Spring 2010 Wednesday, March 31, 2010

description

MVCCalculatorObjective-CDeclaring and implementing objectsSending messages between objects Interface Builder“Wiring up” objects to send messages to each otherSetting up the properties of objectsXcodeManaging all your codeRunning your application in the simulator

Transcript of Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Page 1: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

CS193pSpring 2010

Wednesday, March 31, 2010

Page 2: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Enrollment Closed

You should have received an e-mailIt will confirm your grading status (P/NC or not)As usual, we were oversubscribed for grading optionSorry to anyone who didn’t get the option they wantedIf 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

Page 3: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Communication

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

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

Wednesday, March 31, 2010

Page 4: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Office Hours

AndreasMonday 6pm to 8pmThursday 6pm to 8pmGates B26ABring your Stanford ID card for access to the building

SonaliFriday 11am to 1pmThursday 1pm to 3pmGates B26B

Wednesday, March 31, 2010

Page 5: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Today’s TopicsMVCCalculator

Objective-CDeclaring and implementing objectsSending messages between objects

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

XcodeManaging all your codeRunning your application in the simulator

Wednesday, March 31, 2010

Page 6: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Our Calculator

Model View

Controller

CalculatorViewController

CalculatorBrain

UILabel

UIButton

UIButtonUIButton UIButton

UIButton

UIButton

UIButton

Wednesday, March 31, 2010

Page 7: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

@interface

@end

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 8: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

@interface CalculatorBrain

@end

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 9: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

@end

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 10: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

@end

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 11: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 12: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Specifying void as the return type means that this method returns no value.

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 13: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

The name of this method is “setOperand:”

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 14: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

It takes one argument, a double called “anOperand”

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 15: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

Don’t forget a semicolon here!

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 16: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

This method returns a double.

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 17: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

It takes a pointer to an NSString object as its argument.That’s right, we’re passing an object to this method.

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 18: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

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

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 19: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

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

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

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 20: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

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

It returns a pointer to an NSArray(a collection class in Foundation).

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 21: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

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

The second argument is of type “id”This means “a pointer to *ANY* kind of object!”

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 22: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

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

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 23: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

double operand;{

}

- (void)setOperand:(double)anOperand;

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

@end

ModelHeader File (public API)

Wednesday, March 31, 2010

Page 24: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end

ModelImplementation File (private and public)

Wednesday, March 31, 2010

Page 25: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end

- (double)performOperation:(NSString *)operation{ return aDouble;}

- (void)setOperand:(double)anOperand{

}<code goes here>

<code goes here>

ModelImplementation File (private and public)

Wednesday, March 31, 2010

Page 26: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end

- (double)performOperation:(NSString *)operation{ return aDouble;}

- (void)setOperand:(double)anOperand{

}<code goes here>

<code goes here>

No semicolon this time!

ModelImplementation File (private and public)

Wednesday, March 31, 2010

Page 27: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end

- (double)performOperation:(NSString *)operation{ return aDouble;}

- (void)setOperand:(double)anOperand{

}<code goes here>

[operation sendMessage:argument];

ModelImplementation File (private and public)

Wednesday, March 31, 2010

Page 28: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end

- (double)performOperation:(NSString *)operation{ return aDouble;}

- (void)setOperand:(double)anOperand{

}<code goes here>

[operation sendMessage:argument];

Square brackets mean “send a message.”

ModelImplementation File (private and public)

Wednesday, March 31, 2010

Page 29: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end

- (double)performOperation:(NSString *)operation{ return aDouble;}

- (void)setOperand:(double)anOperand{

}<code goes here>

[operation sendMessage:argument];

This is the object to send the message to(in this case, the NSString called “operation” that was

passed as an argument to performOperation:).

ModelImplementation File (private and public)

Wednesday, March 31, 2010

Page 30: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end

- (double)performOperation:(NSString *)operation{ return aDouble;}

- (void)setOperand:(double)anOperand{

}<code goes here>

[operation sendMessage:argument];

This is the message to send.

ModelImplementation File (private and public)

Wednesday, March 31, 2010

Page 31: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

#import “CalculatorBrain.h”

@implementation CalculatorBrain

@end

- (double)performOperation:(NSString *)operation{ return aDouble;}

- (void)setOperand:(double)anOperand{

}<code goes here>

[operation sendMessage:argument];

And this is its one (in this case) argument.

ModelImplementation File (private and public)

Wednesday, March 31, 2010

Page 32: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Controller

@end

:

#import <UIKit/UIKit.h>

@interface CalculatorViewController UIViewController

IBOutlet UILabel * display;

{

}

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

CalculatorBrain * brain;

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

Wednesday, March 31, 2010

Page 33: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Controller

@end

:

#import <UIKit/UIKit.h>

@interface CalculatorViewController UIViewController

IBOutlet UILabel * display;

{

}

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

CalculatorBrain * brain;

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

Our Controller inherits from UIViewController. UIKit supports MVC primarily

through this class.

Wednesday, March 31, 2010

Page 34: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Controller

@end

:

#import <UIKit/UIKit.h>

@interface CalculatorViewController UIViewController

IBOutlet UILabel * display;

{

}

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

CalculatorBrain * brain;

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

This is going to point to our CalculatorBrain Model

Wednesday, March 31, 2010

Page 35: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Controller

@end

:

#import <UIKit/UIKit.h>

@interface CalculatorViewController UIViewController

IBOutlet UILabel * display;

{

}

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

CalculatorBrain * brain;

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

ViewThese hook up to our

Wednesday, March 31, 2010

Page 36: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Controller

@end

:

#import <UIKit/UIKit.h>

@interface CalculatorViewController UIViewController

IBOutlet UILabel * display;

{

}

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

CalculatorBrain * brain;

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

View

Model

Wednesday, March 31, 2010

Page 37: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

CalculatorViewController.xib

Wednesday, March 31, 2010

Page 38: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

“File’s Owner” is ourController

CalculatorViewController.xib

Wednesday, March 31, 2010

Page 39: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Wednesday, March 31, 2010

Page 40: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Wednesday, March 31, 2010

Page 41: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Wednesday, March 31, 2010

Page 42: Lecture 2#- (Intro to Obj-C, Interface Builder and Xcode)

Xcode

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

Wednesday, March 31, 2010