A couple of weekends ago I had the privelage of attending the “iPhone dev school” by robots and pencils, and taught by JR and Paul.
It was a fun weekend with a tonne of content packed in to 2 days. Here are a few of my notes from the course.
MVC
- Controller interacts with model.
- Controller interacts with view.
- Controller/View interaction occurs through the use of protocols and delegates.
Method Invocation
Greeting greeting = [[Greeting alloc] init];
[greeting greet:@"hello world" name:@"Mo"]
[greeting greet:@"hello world"]
@implementation Greeting
-(void)greet:(NSString *)title name:(NSString *)name
{
}
end
Header File
.h-> header file that defines public interface of object
@protocol TempWeatherServiceDelegate <NSObject>
-(void)didFetchWeather:(Weather *)weather;
end
@interface TempWeatherService : NSObject
-(void)fetchWeather;
@property (nonatomic, weak) id <TempWeatherServiceDelegate> delegate;
end
Implementation File
.m-> implementation file. this file defines the public and non public functions.
@implementation TempWeatherService
@synthesize delegate = _delegate;
-(void)fetchWeather
{
Weather *weather = [[Weather alloc] init];
weather.currentTemperature = -20;
[self.delegate didFetchWeather:weather]
}
Protocols and Delegates
- Controller assigns itself as the view’s delegate.
- View can only have a single delegate.
- View forwards screen interactions to it’s delegate.
- the protocol is the contract that the controller must implement in order to receive calls of specific screen interactions.
- the view has no intimate knowledge of its delegate, only the contract (protocol) defined between it and its delegate.
- protocols are used to achieve polymorphic behaviour.
- allows for non-blocking calls. interesting asynchronous programming model.
TempWeatherService service = [[TempWeatherService alloc] init];
service.delegate = self;
[service fetchWeather]
Notifications (event aggregator)
- NSNotifications can go to any number of subscribers.
[NSNotificationCenter defaultCenter]
-(void)addObserver:(id)observer selector:(SEL)methodToCallIfSomethingHappens name:(NSString)name object:(id)sender