-(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"];
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。