15
Objective-C Classes, Protocols, Delegates for iOS Beginners Adam Musial-Bright, @a_life_hacker

Objective-C for Beginners

Embed Size (px)

DESCRIPTION

Objective-C for Beginners: Classes, Protocols, Delegates

Citation preview

Page 1: Objective-C for Beginners

Objective-CClasses, Protocols, Delegates

for iOS Beginners

Adam Musial-Bright, @a_life_hacker

Page 2: Objective-C for Beginners

Classes & Objects

Objective-C is an object oriented language - this is good, because you can model things.

Page 3: Objective-C for Beginners

Like an architect creates a plan for a house ...

... you create a plan for your software.

Modeling the world

http://www.freedigitalphotos.net

Page 4: Objective-C for Beginners

So plan with classes

http://www.freedigitalphotos.net

Window

Door

Roof

Wall

Basement

House

A class for ...

Page 5: Objective-C for Beginners

Now translated to iOS

Window Door Basement

... could be a ...

NSView NSButton NSString

Page 6: Objective-C for Beginners

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.

Page 7: Objective-C for Beginners

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

Page 8: Objective-C for Beginners

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

Page 9: Objective-C for Beginners

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

Page 10: Objective-C for Beginners

Class take-away #2

connect view elements with controller objects to

outlets

Page 11: Objective-C for Beginners

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.

Page 12: Objective-C for Beginners

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

Page 13: Objective-C for Beginners

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.

Page 14: Objective-C for Beginners

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 ...

Page 15: Objective-C for Beginners

Thank you + Q&AAdam Musial-Bright, @a_life_hacker

Presentationhttp://www.slideshare.net/musial-bright

Codehttps://github.com/musial-bright/iOSBeginners