2012-11-15 81 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

相關問題