Upload
adam-musial-bright
View
1.444
Download
0
Embed Size (px)
DESCRIPTION
Objective-C for Beginners: Classes, Protocols, Delegates
Citation preview
Objective-CClasses, Protocols, Delegates
for iOS Beginners
Adam Musial-Bright, @a_life_hacker
Classes & Objects
Objective-C is an object oriented language - this is good, because you can model things.
Like an architect creates a plan for a house ...
... you create a plan for your software.
Modeling the world
http://www.freedigitalphotos.net
So plan with classes
http://www.freedigitalphotos.net
Window
Door
Roof
Wall
Basement
House
A class for ...
Now translated to iOS
Window Door Basement
... could be a ...
NSView NSButton NSString
Class example#import <Foundation/Foundation.h>
@interface Window : NSObject
- (void)open;
- (void)close;
- (BOOL)isOpen;
- (UIImage *)image;
@end
#import "Window.h"
@interface Window()@property NSString *state;@end
@implementation Window
@synthesize state = _state;
- (id)init { if (self = [super init]) { self.state = @"closed"; return self; } else { return nil; }}
- (void)open { self.state = @"open";}...http://www.freedigitalphotos.net
The interface and the implementation come always as a pair.
Classes and Objects#import <Foundation/Foundation.h>
@interface Window : NSObject
- (void)open;
- (void)close;
- (BOOL)isOpen;
- (UIImage *)image;
@end
#import "Window.h"
@interface Window()@property NSString *state;@end
@implementation Window
@synthesize state = _state;
- (id)init { if (self = [super init]) { self.state = @"closed"; return self; } else { return nil; }}
- (void)open { self.state = @"open";}
...
http://www.freedigitalphotos.net
The interface and the implementation come always as pair.
Window *northWindow = [[Window alloc] init];Window *southWindow = [[Window alloc] init];
[northWindow open];
[northWindow isOpen]; // -> open[southWindow isOpen]; // -> closed
two independent objects
open the north window
the south window stays closed
Class syntax & semantics#import “MyImportedLibraryHeader.h”
@interface MyClassName : SuperClass
+ (void)classMethod;
- (void)instenceMethod;
- (NSString *)methodWithReturnValue;
- (void)methodWithParameter:(NSString *)text;
@end
#import "MyClassName.h"
@interface MyClassName()// private interface@end
@implementation MyClassName
- (id)init { if (self = [super init]) { // do some stuff here return self; } else { return nil; }}
- (void)instanceMethod { NSLog(@”log output”);}
...
@end
Class take-away #1#import <Foundation/Foundation.h>
@interface Window : NSObject
- (void)open;
- (void)close;
- (BOOL)isOpen;
- (UIImage *)image;
@end
#import "Window.h"
@interface Window()@property NSString *state;@end
@implementation Window
@synthesize state = _state;
- (id)init { if (self = [super init]) { self.state = @"closed"; return self; } else { return nil; }}
- (void)open { self.state = @"open";}
...
the interface defines public methods like “open”, or
“close” the window
private interface are only visible in the implementation
prepare the object state when initialized
access properties easily with the “.” notation
synthesize properties
Class take-away #2
connect view elements with controller objects to
outlets
Delegate & Protocol
http://www.freedigitalphotos.net http://www.freedigitalphotos.net
Do not call me all the time......I call you back
when I am ready!
There are moments where we want anobjects to send a message to us - a
callback.
Define a protocol#import <Foundation/Foundation.h>
@protocol NoiseDelegate <NSObject>@required- (void)makesNoise:(NSString *)noise;@end
@interface Window : NSObject { id <NoiseDelegate> delegate;}
@property id delegate;
- (void)open;
- (void)close;
- (BOOL)isOpen;
- (UIImage *)image;
@end
the protocol defines the callback method
“makeNoise”
here we have a delegate and a
delegate property
@implementation Window
@synthesize delegate = _delegate;
...
- (void)close { self.state = @"closed"; int r = arc4random() % 4; if (r == 0) { [self.delegate makesNoise:@"bang!"]; } else { [self.delegate makesNoise:@""]; }}
... 25% of closed windows make a “bang!” noise
synthesize and execute your “makeNoise” callback
Use the delegate#import <UIKit/UIKit.h>#import "Window.h"
@interface HouseViewController : UIViewController <NoiseDelegate> {
IBOutlet UIButton *northWindowButton; IBOutlet UIButton *southWindowButton; IBOutlet UIImageView *northImageView; IBOutlet UIImageView *southImageView; IBOutlet UILabel *noiseLabel;}@end use the delegate in the class
interface where the callback should be executed
@implementation HouseViewController
...
[self.northWindow setDelegate:self];[self.southWindow setDelegate:self];
...
- (void)makesNoise:(NSString *)noise { noiseLabel.text = noise;}
...implement the callback required
by the delegate
set the house controller as a window delegate
... and make some noise when the window is closed.
Protocol take-away
@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]; ...}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { ...}
...
set a delegate to receive a response, connection error or
timeouts
... send a asynchronous HTTP request ...
Thank you + Q&AAdam Musial-Bright, @a_life_hacker
Presentationhttp://www.slideshare.net/musial-bright
Codehttps://github.com/musial-bright/iOSBeginners