lecture 2#- (intro to obj-c, interface builder and xcode)
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 simulatorTRANSCRIPT
CS193pSpring 2010
Wednesday, March 31, 2010
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
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
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
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
Our Calculator
Model View
Controller
CalculatorViewController
CalculatorBrain
UILabel
UIButton
UIButtonUIButton UIButton
UIButton
UIButton
UIButton
Wednesday, March 31, 2010
@interface
@end
ModelHeader File (public API)
Wednesday, March 31, 2010
@interface CalculatorBrain
@end
ModelHeader File (public API)
Wednesday, March 31, 2010
#import <Foundation/Foundation.h>
@interface CalculatorBrain : NSObject
@end
ModelHeader File (public API)
Wednesday, March 31, 2010
#import <Foundation/Foundation.h>
@interface CalculatorBrain : NSObject
double operand;{
}
@end
ModelHeader File (public API)
Wednesday, March 31, 2010
#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
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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#import “CalculatorBrain.h”
@implementation CalculatorBrain
@end
ModelImplementation File (private and public)
Wednesday, March 31, 2010
#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
#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
#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
#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
#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
#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
#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
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
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
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
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
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
CalculatorViewController.xib
Wednesday, March 31, 2010
“File’s Owner” is ourController
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