Objective-C for Beginners

3,041 views

Published on

Objective-C for Beginners: Classes, Protocols, Delegates

Published in: Technology
2 Comments
2 Likes
Statistics
Notes
No Downloads
Views
Total views
3,041
On SlideShare
0
From Embeds
0
Number of Embeds
304
Actions
Shares
0
Downloads
104
Comments
2
Likes
2
Embeds 0
No embeds

No notes for slide

Objective-C for Beginners

  1. 1. Objective-CClasses, Protocols, Delegates for iOS Beginners Adam Musial-Bright, @a_life_hacker
  2. 2. Classes & ObjectsObjective-C is an object oriented language -this is good, because you can model things.
  3. 3. Modeling the worldLike an architect creates a plan for a house ... http://www.freedigitalphotos.net... you create a plan for your software.
  4. 4. So plan with classesA class for ... Roof Window Wall Door House http://www.freedigitalphotos.net Basement
  5. 5. Now translated to iOS Window Door Basement ... could be a ... NSView NSButton NSString
  6. 6. Class exampleThe interface and the implementation come always as a pair. #import <Foundation/Foundation.h> #import "Window.h" @interface Window : NSObject @interface Window() @property NSString *state; - (void)open; @end - (void)close; @implementation Window - (BOOL)isOpen; @synthesize state = _state; - (UIImage *)image; - (id)init { if (self = [super init]) { @end self.state = @"closed"; return self; } else { return nil; } } - (void)open { self.state = @"open"; } http://www.freedigitalphotos.net ...
  7. 7. Classes and Objects#import <Foundation/Foundation.h> #import "Window.h"@interface Window : NSObject @interface Window() @property NSString *state;- (void)open; @end- (void)close; @implementation Window- (BOOL)isOpen; @synthesize state = _state;- (UIImage *)image; - (id)init { if (self = [super init]) {@end self.state = @"closed"; two independent return self; Window *northWindow = [[Window alloc] init]; } else { objects Window *southWindow = [[Window alloc] init]; return nil; } [northWindow open]; open the north window } [northWindow isOpen]; // -> open - (void)open { [southWindow isOpen]; // -> closed the south window self.state = @"open"; stays closed } ... The interface and the http://www.freedigitalphotos.net implementation come always as pair.
  8. 8. Class syntax & semantics#import “MyImportedLibraryHeader.h” #import "MyClassName.h"@interface MyClassName : SuperClass @interface MyClassName() // private interface+ (void)classMethod; @end- (void)instenceMethod; @implementation MyClassName- (NSString *)methodWithReturnValue; - (id)init { if (self = [super init]) {- (void)methodWithParameter:(NSString *)text; // do some stuff here return self;@end } else { return nil; } } - (void)instanceMethod { NSLog(@”log output”); } ... @end
  9. 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. 10. Class take-away #2 connect view elements with controller objects to outlets
  11. 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 - ahttp://www.freedigitalphotos.net callback. http://www.freedigitalphotos.net
  12. 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. 13. Use the delegate ... and make some noise when the window is closed.#import <UIKit/UIKit.h>#import "Window.h"@interface HouseViewController : @implementation HouseViewControllerUIViewController <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. 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 ...}...
  15. 15. Presentation http://www.slideshare.net/musial-bright Code https://github.com/musial-bright/iOSBeginnersThank you + Q&A Adam Musial-Bright, @a_life_hacker

×