Inf*_*mes 8 nsnotifications nsnotificationcenter ios
我知道为什么不收到通知的标准原因:
我很高兴地说,我很确定这些都不会发生.我想最有可能的是,对象在某些时候被无效并重新创建,但它在初始化时注册通知.
这是我注册的地方:
/**
* 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;
}
Run Code Online (Sandbox Code Playgroud)
这是我发布通知的地方:
/**
* 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是未被调用的方法:
/**
* 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.");
}];
}
Run Code Online (Sandbox Code Playgroud)
这是控制台澄清我注册并发布通知:
2012-11-15 13:27:50.958 iCloud Custom [11269:907]刚刚注册为'com.andbeyond.jamesvalaitis.notesChanged'
2012-11-15 13:28:24.184 iCloud Custom [11269:907]刚发布'com.andbeyond.jamesvalaitis.notesChanged'
Din*_*aja 21
我想我已经找到了答案.您正在UIDocument名为file的文件中创建通知NotesDocument.m.因此,在创建观察者时,将对象设置为self.这意味着NotesDocument对象.但是当你post the notification,你正在发送对象nil.所以it wont observe the notification根据文件.解决这个问题的简单方法是nil在创建通知时需要设置对象.否则你需要通过NotesDocument Object.
查看addObserver通知方法的以下图像和参数详细信息.

检查notificationSender参数.观察者想要接收通知的对象; 也就是说,只有此发件人发送的通知才会传递给观察者.
| 归档时间: |
|
| 查看次数: |
11237 次 |
| 最近记录: |