我知道標準的理由,爲什麼通知不會被接受: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.
將noteChanged方法添加到帖子... – chrislhardin
您的選擇器仍然像這樣@selector(noteChanged)..那麼它將如何調用此 - (void)noteChanged:(NSNotification *)notification ???你需要改變你的選擇器像這樣@selector(noteChanged :) –
@DineshRaja:嗨Dinesh和James,我面臨一個類似的問題,並已經通過了這篇文章的所有答案,應用這些建議;仍然有事情沒有爲我制定。 我能夠成功發佈通知並聲明觀察者。但是檢查的邏輯不會被執行。 我應該如何分享代碼?或者我應該發表一個相當重複的問題? –