Objective c中有委托模式
变量转换器.h
@protocol VariableChangedProtocol
-(void) variableDidChange;
@end
@interface VariableChangerClass : NSObject
{
id<VariableChangedProtocol> delegate;
int exampleVariable;
}
@property int exampleVariable;
-(id) initWithDelegate:(id<VariableChangedProtocol>) target;
@end
变量转换器.m
@implementation
@synthesize exampleVariable;
-(id) initWithDelegate:(id<VariableChangedProtocol>) target
{
self = [super init];
if (self)
{
delegate = target;
}
}
-(void) setExampleVariable:(int) newValue
{
if(newValue != exampleVariable)
exampleVariable = newValue;
[delegate variableDidChange];
}
@end
VariableChangedListener.m
@implementation VariableChangedListener
-(id) init
{
self = [super init];
if (self)
{
VariableChangerClass *v = [[VariableChangerClass alloc] initWithDelegate:self];
}
return self;
}
-(void) variableDidChange
{
NSLog(@"The variable changed");
}
@end