Cocoa Custom Notification Example

后端 未结 3 800
北荒
北荒 2020-11-28 20:19

Can someone please show me an example of a Cocoa Obj-C object, with a custom notification, how to fire it, subscribe to it, and handle it?

3条回答
  •  粉色の甜心
    2020-11-28 21:07

    Make sure to unregister notification (observer) when your object is deallocated. Apple documentation states: "Before an object that is observing notifications is deallocated, it must tell the notification center to stop sending it notifications".

    For Local Notifications the next code is applicable:

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    And for observers of distributed notifications:

    [[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
    

提交回复
热议问题