ios – NSNotificationCenter观察者未收到通知

ios – NSNotificationCenter观察者未收到通知,第1张

概述我知道为什么不收到通知的标准原因: >取消分配或取消已注册的对象. >以观察者身份移除对象. >未注册为观察员. >注册错误通知或发布错误通知. 我很高兴地说,我很确定这些都不会发生.我想最有可能的是,对象在某些时候被无效并重新创建,但它在初始化时注册通知. 这是我注册的地方: /** * initialises an object with a unique file url * * 我知道为什么不收到通知的标准原因:

>取消分配或取消已注册的对象.
>以观察者身份移除对象.
>未注册为观察员.
>注册错误通知或发布错误通知.

我很高兴地说,我很确定这些都不会发生.我想最有可能的是,对象在某些时候被无效并重新创建,但它在初始化时注册通知.

这是我注册的地方:

/** *  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’

The whole project can be found here.

解决方法 我想我已经找到了答案.您正在名为Notesdocument.m的UIdocument文件中创建通知.因此,在创建观察者时,将对象设置为self.这意味着Notesdocument对象.但是当您发布通知时,您将发送对象为零.所以它不会根据文档观察通知.解决这个问题的简单方法是在创建通知时需要将对象设置为nil.否则,您需要传递Notesdocument对象.

查看addobserver通知方法的以下图像和参数详细信息.

检查notificationSender参数.观察者想要接收通知的对象;也就是说,只有此发件人发送的通知才会传递给观察者.

总结

以上是内存溢出为你收集整理的ios – NSNotificationCenter观察者未收到通知全部内容,希望文章能够帮你解决ios – NSNotificationCenter观察者未收到通知所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1101902.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存