iOS – Event Kit编程监听通知

我想观察日历应用程序的变化,所以我注册了EKEventStoreChangedNotification通知。 但是,我是否需要让EKEventStore对象“活着”才能收到此通知? 我想我正在初始化视图控制器中的EKEventStore对象来检索一些事件。 然后我将popup导航堆栈的这个视图控制器,视图控制器将被释放,因此EKEventStore对象将被解除分配。

不,您不需要保持EKEventStore对象处于活动状态,因为您已经使用名为eventStore的 EKEventStore对象注册了EKEventStoreChangedNotification

 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(storeChanged:) name:EKEventStoreChangedNotification object:eventStore]; 

请参阅此处以获得更多的疑问

对于swift 3.x,使用如下

 NotificationCenter.default.addObserver(self, selector: #selector(ViewController.storeChanged(_:)), name: NSNotification.Name.EKEventStoreChanged, object: eventStore) ... ... ... //Method func storeChanged(_ nsNotification: NSNotification) { //do your stuff }