-(void)bind:(NSString *)binding toObject:(id)observableController withKeyPath:(NSString *)keyPath options:(NSDictionary *)options
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
bindV = [[bindView alloc] init];
btn = [[HYMBtn alloc] init];
bindV.bageCount = 0;
[btn setBindView:bindV];
}
- (IBAction)btnClick:(id)sender {
bindV.bageCount ++;
NSLog(@"=====%d",bindV.bageCount);
NSLog(@"+++++++%d",btn.count);
//当bindV.bageCount变化的时候,btn.count也是随着变化。
}
@implementation HYMBtn
- (void)setBindView:(bindView *)bindView {
//将自身.count属性绑定到bindView.bageCount属性上,当bageCount发生变化的时候,自身.count属性也随着变化
[self bind:@"count" toObject:bindView withKeyPath:@"bageCount" options:nil];
}
//解除绑定
-(void)dealloc {
[self unbind:@"count"];
}
网友评论