I guess KVO would be sufficient
small example:
Code:
@interface myModel : NSObject {
NSInteger funnyInt;
}
-(void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
-(void)removeObserver:(NSObject *)observer forKeyPath:(NSString *);
Implementation:
Code:
-(void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context{
[super addObserver:observer forKeyPath:keyPath options:options context:context];
}
-(void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath{
[super removeObserver:observer forKeyPath:keyPath];
}
Code:
@interface myObserver : NSObject {
}
-(id)initWithMyModel:(myModel*)mm
-(void)myMethod:(NSInteger)x;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
implementation:
Code:
-(id)initWithMyModel:(myModel*)mm{
if(self=[super init]){
[mm addObserver:self forKeyPath:@"funnyInt" options:NSKeyValueObservingOptionNew context:nil];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if(object.funnyInt<=1){
[self myMethod:object.funnyInt];
}
}
-(void)myMethod:(NSInteger)x{
NSLog("funnyint changed: %d", x);
}
by adding 'myObserver' as observer of the attribute 'funnyInt' of the class 'myModel' via the method:
Code:
addObserver:self forKeyPath:@"funnyInt" options:NSKeyValueObservingOptionNew context:nil;
automatically calls the method
Code:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
when funnyInt changes via the method:
[mm setValue:10 forKey:"funnyInt"];
experiment a bit with it, and you'll get the hang of it