KVO使用的方法
/** * Used by Foundation's Key-Value Observing. * * @warning Do not call this function yourself. */ OBJC_EXPORT Class objc_duplicateClass(Class original, const char *name, size_t extraBytes) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
UIViewController [self addObserver:self forKeyPath:@"intValue" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil]; self.intValue = 1230; /// 一般的属性也会触发该方法调用 storyboard mswitch psegment sswitch view - (void)setValue:(id)value forKeyPath:(NSString *)keyPath { [super setValue:value forKeyPath:keyPath]; NSLog(@"通过KVC设置值key:%@", keyPath); } /// 一般的属性也会触发该方法调用 storyboard mswitch psegment sswitch view - (void)setValue:(id)value forKey:(NSString *)key { [super setValue:value forKey:key]; NSLog(@"设置Key-Value对key:%@", key); } /// 这个函数在调用addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:之后对应属性变化才会调用 - (void)didChangeValueForKey:(NSString *)key { NSLog(@"已经改变key:%@",key); [super didChangeValueForKey:key]; } /// 这个函数在调用addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:之后对应属性变化才会调用 - (void)willChangeValueForKey:(NSString *)key { NSLog(@"即将改变key:%@",key); [super willChangeValueForKey:key]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { NSLog(@"接收到消息改变key:%@ %@",keyPath,change); }