2012-11-15 77 views
6

我知道标准的理由,为什么通知不会被接受:NSNotificationCenter观察员没有收到通知

  • 取消分配或注册的对象抵消。
  • 以观察者身份移除对象。
  • 未注册为观察者。
  • 注册错误的通知或发布错误的通知。

我可以高兴地说,我敢肯定,这些都没有发生。我想最有可能的是该对象在某个时候被取消并重新创建,但它在初始化时注册了通知。

这里是我注册:

/** 
* initialises an object with a unique file url 
* 
* @param url       the url to set as the file url 
*/ 
- (id)initWithFileURL:(NSURL *)url 
{ 
    if (self = [super initWithFileURL:url]) 
    { 
     self.entries     = [[NSMutableArray alloc] init]; 

     // we want to be notified when a note has changed 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(noteChanged) 
                name:@"com.andbeyond.jamesvalaitis.notesChanged" 
                object:self]; 

     NSLog(@"Just registered for 'com.andbeyond.jamesvalaitis.notesChanged'"); 
    } 

    return self; 
} 

这里就是我发布通知:

/** 
* save the note content 
*/ 
- (void)saveNote 
{ 
    if (_isChanged) 
    { 
     // save to the text view to the note's contents 
     self.note.noteContent   = self.noteView.text; 

     // post a notification that we changed the notes 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"com.andbeyond.jamesvalaitis.notesChanged" object:nil]; 

     NSLog(@"Just posted 'com.andbeyond.jamesvalaitis.notesChanged'"); 

     // make sure we know it's already saved 
     _isChanged       = NO; 
    } 
} 

这是不被调用的方法:

/** 
* called when a note has changed 
*/ 
- (void)noteChanged:(NSNotification *)notification 
{ 
    NSLog(@"Just received for 'com.andbeyond.jamesvalaitis.notesChanged'"); 

    // save the notes 
    [self saveToURL:self.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) 
    { 
     if (success) 
      NSLog(@"Note updated."); 
    }]; 
} 

这是控制台澄清我注册并发布通知:

2012年11月15日13:27:50.958的iCloud自定义[11269:907]刚刚登记 'com.andbeyond.jamesvalaitis.notesChanged'

2012年11月15日13:28:24.184的iCloud自定义[11269:907]刚刚发布 'com.andbeyond.jamesvalaitis.notesChanged'

The whole project can be found here.

+1

将noteChanged方法添加到帖子... – chrislhardin

+0

您的选择器仍然像这样@selector(noteChanged)..那么它将如何调用此 - (void)noteChanged:(NSNotification *)notification ???你需要改变你的选择器像这样@selector(noteChanged :) –

+0

@DineshRaja:嗨Dinesh和James,我面临一个类似的问题,并已经通过了这篇文章的所有答案,应用这些建议;仍然有事情没有为我制定。 我能够成功发布通知并声明观察者。但是检查的逻辑不会被执行。 我应该如何分享代码?或者我应该发表一个相当重复的问题? –

回答

19

我想我已经找到了答案。您正在创建名为NotesDocument.mUIDocument文件中的通知。所以当你创建一个观察者时,你将对象设置为self。这意味着NotesDocument对象。但是当你post the notification,你发送的对象为nil。所以按照文档it wont observe the notification克服这个问题的简单方法是创建通知时需要将对象设置为nil。否则,您需要通过NotesDocument Object

请查看addObserver通知方法的以下图像和参数详细信息。

enter image description here

退房的notificationSender参数。观察者希望接收其通知的对象;也就是只有这个发送者发送的通知被传送给观察者。

+0

我一直是个白痴。当我注册通知时,我完全不是故意将对象设置为自己的。 这现在完美。 非常感谢你。 –

+0

我刚刚有完全相同的问题,并不能相信我很快找到答案。这是一件很小的事情;所以很容易被忽视,所以很难发现。非常感谢你。 – halfwaythru

+0

大约花了20分钟的时间,我认为我没有'nil'作为观察者的对象,而且它是'self'。感谢您发布这篇文章,以便检查我所期望的粉红色属性是否正确。 :) – kyleturner

0

更改如下:

... 
selector:@selector(noteChanged:) 

... 
- (void) noteChanged:(NSNotification *) notification 
... 
+0

只是做了这个改变,并没有奏效。不管怎样,谢谢你。 –

+0

尝试在两个位置使用静态字符串,而不是通知的其他字符串。它们看起来完全一样,但可能有些奇怪。 – chrislhardin

相关问题