我知道标准的理由,为什么通知不会被接受: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,我面临一个类似的问题,并已经通过了这篇文章的所有答案,应用这些建议;仍然有事情没有为我制定。 我能够成功发布通知并声明观察者。但是检查的逻辑不会被执行。 我应该如何分享代码?或者我应该发表一个相当重复的问题? –