本文共 655 字,大约阅读时间需要 2 分钟。
在iOS中,有两种技术是属于观察者模式的。
它们分别是通知和KVO。
NSNotificationCenter *notificantionCenter = [NSNotificationCenter defaultCenter];
[notificantionCenter addObserver:self selector:@selector(update) name:@"notifiName" object:nil];
发送通知
NSNotification *nofitication = [NSNotification notificationWithName:@"nofitiName" object:nil];
[notificantionCenter postNotification:nofitication];
[student addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
回调方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void*)context{
}
转载地址:http://iiqyb.baihongyu.com/