9. Class take-away #1
#import <Foundation/Foundation.h> #import "Window.h" private interface are only
visible in the implementation
@interface Window : NSObject @interface Window()
@property NSString *state;
- (void)open; @end
- (void)close; @implementation Window synthesize properties
- (BOOL)isOpen; @synthesize state = _state;
- (UIImage *)image; - (id)init {
if (self = [super init]) {
@end self.state = @"closed";
the interface defines public return self;
methods like “open”, or } else { prepare the object state
return nil;
“close” the window }
when initialized
}
- (void)open {
self.state = @"open";
}
access properties easily
...
with the “.” notation
10. Class take-away #2
connect view elements
with controller objects to
outlets
11. Delegate & Protocol
Do not call me all the time...
...I call you back
when I am ready!
There are moments
where we want an
objects to send a
message to us - a
http://www.freedigitalphotos.net
callback. http://www.freedigitalphotos.net
12. Define a protocol
the protocol defines the
#import <Foundation/Foundation.h>
callback method
@protocol NoiseDelegate <NSObject> “makeNoise”
@required
- (void)makesNoise:(NSString *)noise;
@end
@implementation Window
@interface Window : NSObject {
@synthesize delegate = _delegate;
id <NoiseDelegate> delegate;
}
here we have a ... synthesize and execute your
@property id delegate; delegate and a “makeNoise” callback
- (void)close {
self.state = @"closed";
- (void)open; delegate property int r = arc4random() % 4;
if (r == 0) {
- (void)close;
[self.delegate makesNoise:@"bang!"];
} else {
- (BOOL)isOpen;
[self.delegate makesNoise:@""];
}
- (UIImage *)image;
}
@end
... 25% of closed windows
make a “bang!” noise
13. Use the delegate
... and make some noise when the window is closed.
#import <UIKit/UIKit.h>
#import "Window.h"
@interface HouseViewController : @implementation HouseViewController
UIViewController <NoiseDelegate> {
...
IBOutlet UIButton *northWindowButton;
IBOutlet UIButton *southWindowButton; [self.northWindow setDelegate:self];
IBOutlet UIImageView *northImageView; [self.southWindow setDelegate:self];
IBOutlet UIImageView *southImageView;
}
IBOutlet UILabel *noiseLabel; set the house controller as a
@end window delegate
use the delegate in the class ...
interface where the callback
should be executed
- (void)makesNoise:(NSString *)noise {
noiseLabel.text = noise;
}
implement the callback required
...
by the delegate
14. Protocol take-away
... send a asynchronous HTTP request ...
@implementation WebService : NSObject
- (void)sendRequest {
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *urlRequest = [NSURLRequest
requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:30];
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:urlRequest delegate:controller];
...
} set a delegate to receive a
- (void)connection:(NSURLConnection *)connection
response, connection error or
didReceiveResponse:(NSURLResponse *)response { timeouts
...
}
...