iOS设计模式(六) 观察者模式

来源:互联网 发布:电话营销数据库 编辑:程序博客网 时间:2024/06/05 06:07

在iOS中,有两种技术是属于观察者模式的。

它们分别是通知和KVO。

通知

注册通知

    NSNotificationCenter *notificantionCenter = [NSNotificationCenterdefaultCenter];

    [notificantionCenter addObserver:selfselector:@selector(update)name:@"notifiName"object:nil];

    

发送通知

    NSNotification *nofitication = [NSNotificationnotificationWithName:@"nofitiName"object:nil];

    [notificantionCenter postNotification:nofitication];


KVO

注册KVO

    [student addObserver:selfforKeyPath:@"name"options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOldcontext:nil];

回调方法

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void*)context{

    

    }



0 0
原创粉丝点击