NSNotification 未发送/接收
我一整天都在试图解决这个问题,我开始变得绝望...... 所以这是我的问题:
我有一个显示文件列表的表视图控制器。用户可以将新文件下载到该列表中。我在一个单独的类中异步处理下载。下载新文件后,我想通知表视图控制器,以便它可以更新列表。委托模式并不适合这里,因为必须通知多个实例,所以我想使用 NSNotificationCenter。
在视图控制器的 viewDidAppear: 方法中,我注册为观察者:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(downloadComplete:)
name:kDownloadCompleteNotification
object:nil];
这在发布通知之前肯定会调用,因为用户必须点击视图控制器上的添加按钮才能开始新的下载。下载完成后,我发布通知(在 DownloadManager 类中):
[[NSNotificationCenter defaultCenter]
postNotificationName:kDownloadCompleteNotification
object:self];
但是,视图控制器中的通知处理程序方法永远不会被调用。我添加了断点,但它没有被调用。我还尝试将对象设置为零。没有区别。
- (void)downloadComplete:(NSNotification *)notification {
NSLog(@"Inserting new files into table view.");
}
注册和发布通知都是在主线程上完成的(无论如何我认为这并不重要)。视图控制器没有被释放或者什么的。据我所知,我没有犯任何错字。名称字符串是相同的常量。
如果在注册为观察者时将 name 参数设置为 nil,我会收到所有类型的(系统)通知,但不会收到我自己的通知。但是,如果我在注册后立即发布测试通知(在 viewDidAppear 中),它就会起作用。
我不知道我还应该检查什么...非常感谢任何提示。谢谢!
I've been trying to figure this out for a whole day now and I'm starting to become desperate...
So here is my problem:
I have a table view controller showing a list of files. The user can download new files to this list. I handle the downloads asynchronously in a separate class. When a new file has been downloaded I want to notify the table view controller, so it can update the list. The delegate pattern doesn't really fit here, because multiple instances have to be notified, so I want to use NSNotificationCenter.
In the view controller's viewDidAppear: method I register as an observer:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(downloadComplete:)
name:kDownloadCompleteNotification
object:nil];
This is called definitely before posting the notification, because the user has to tap the add button on the view controller to start a new download. When the download is done I post the notification (in the DownloadManager class):
[[NSNotificationCenter defaultCenter]
postNotificationName:kDownloadCompleteNotification
object:self];
However, my notification handler method in the view controller is never called. I added breakpoints and it's just not called. I also tried to set the object to nil. No difference.
- (void)downloadComplete:(NSNotification *)notification {
NSLog(@"Inserting new files into table view.");
}
Both, registering and posting the notification is done on the main thread (I don't think that matters anyway). The view controller is not released or anything. I didn't make any typo as far as I can tell. The name strings are the same constant.
If set the name argument to nil when registering as an observer, I receive all kind of (system) notifications, but not my own ones. However if I post a test notification right after registering (in viewDidAppear) it works.
I don't know what else I should check... Any hint is very appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论