我知道为什么不收到通知的标准原因:
>取消分配或取消已注册的对象.
>以观察者身份移除对象.
>未注册为观察员.
>注册错误通知或发布错误通知.
我很高兴地说,我很确定这些都不会发生.我想最有可能的是,对象在某些时候被无效并重新创建,但它在初始化时注册通知.
这是我注册的地方:
/** * 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 Custom[11269:907] Just registered for
‘com.andbeyond.jamesvalaitis.notesChanged’2012-11-15 13:28:24.184 iCloud Custom[11269:907] Just posted
‘com.andbeyond.jamesvalaitis.notesChanged’