让 iOS 的键值观察编程变得更简单
#import "KVOHelper.h"
- (id)init {
if (self = [super init]) {
[KVOHelper mixinHandlerToKVOContextForClass:self.class];
[self.account addObserver:self forKeyPath:@"name" options:0 context:@selector(onAccountNameChanged:)];
self.account.name = @"My Account"; // this will trigger onAccountNameChanged:
}
return self;
}
- (void)onAccountNameChanged:(NSDictionary)change {
NSString *accountName = [change objectForKey:@"new"];
self.accountNameLabel.text = [NSString stringWithFormat:@"%@", accountName];
}