【发布时间】:2010-07-07 21:30:05
【问题描述】:
在我的 iPad 应用程序中,我在一个课程中注册了一个通知:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(selectedList:) name:@"TTSelectedList" object:nil];
我的selectedList: 方法如下所示:
- (void)selectedList:(NSNotification*)notification
{
NSLog(@"received notification");
}
然后在另一个班级(UITableViewController)中,我在选择一行时发布该通知:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"posting notification");
[[NSNotificationCenter defaultCenter] postNotificationName:@"TTSelectedList" object:nil];
}
我可以确认正在发布通知,因为“发布通知”已记录到控制台,但从未调用过“已收到通知”,这意味着未收到通知并且未调用选择器。我不知道是什么原因造成的。
谢谢
【问题讨论】:
-
有点愚蠢,但这让我困惑了一段时间。我遇到过同样的问题。就我而言,我没有注意到子类的方法与我试图在父类中声明的选择器具有相同的选择器。
-
我自己也遇到过这个问题,也很愚蠢:我发布的通知是小写字母而不是大写字母。它们区分大小写!
-
在我的例子中,我在本地方法中创建了一个对象,所以观察者类在通知期间从另一个类触发。你可以检查这个:stackoverflow.com/questions/67328439/…
标签: objective-c cocoa-touch ipad nsnotification nsnotificationcenter