Notification基本操作

    技术2022-05-19  22

    基本的思想就是一个接收信号,将信号分发给不同相应对象和方法的一个过程。

     

    1 首先要获得一个NSNotificationCenter的单例。

    NSNotificationCenter *ntfCenter = [NSNotificationCenter defaultCenter];

     

    2 在 NSNotificationCenter 中增加观察者

    [ntfCenter addObserver:self selector:@selector(getNtf) name:@"chgValue" object:self]; [ntfCenter addObserver:self selector:@selector(getNtf2) name:@"chgValue" object:self];

    第一个参数决定哪个对象来接收notification。

    第二个参数决定接收到notification后,具体执行哪个函数。

    第三个参数决定接收的notification的名字。如果指定为nil,则接收任何名字的notification。

    第四个参数决定接收notification的发送者的名字。如果指定为nil,则会接收任何发送者发出的notification。

     

    3 发送notification

    NSNotification *ntf = [NSNotification notificationWithName:@"chgValue" object:self]; NSNotificationCenter *ntfCenter = [NSNotificationCenter defaultCenter]; [ntfCenter postNotification:ntf];


    最新回复(0)