How to post and receive an NSNotifications (Objective C) | Notifications (in Swift)?

后端 未结 3 1405
遥遥无期
遥遥无期 2020-12-03 02:56

Is there an easy-to-grock pattern how to send a NSNotification (Objective C) | Notification (in Swift) and how to receive one? Code snippet? The docs write like 150 pages on

3条回答
  •  情话喂你
    2020-12-03 03:44

    Send a notification:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"MyCacheUpdatedNotification" object:self];
    

    Receive it:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cacheUpdated:) name:@"MyCacheUpdatedNotification" object:nil];
    

    Act on it:

    - (void)cacheUpdated:(NSNotification *)notification {
    [self load];
    }
    

    And dispose of it:

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

提交回复
热议问题