发送NSNotification时无法识别的选择器

Bil*_*eau 3 selector nsnotificationcenter ios

我试图在iOS应用程序中实现可折叠的表视图.为此,我在部分标题中设置了一个手势识别器,它将激活NSNotification到父控制器,然后刷新显示扩展视图的视图.

一切正常,直到父控制器收到其消息,这将导致发生以下错误:

'+[MasterViewController receiveTestNotification:]: unrecognized selector sent to class 0xa92a8'
Run Code Online (Sandbox Code Playgroud)

我在这个网站上环顾四周,发现了一些与这个错误有关的帖子,但据我所知,我没有犯这些错误.

我的注册发生在控制器的初始化中,如下所示:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"RefreshNavigation" object:nil];
Run Code Online (Sandbox Code Playgroud)

我想调用的接收方法有这个签名:

- (void) receiveTestNotification:(NSNotification *) notification
Run Code Online (Sandbox Code Playgroud)

我发送这样的通知,这是在UIView的自定义子类中,我用作节头:

[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshNavigation" object:self];
Run Code Online (Sandbox Code Playgroud)

我发现的例子指出了这种确切的配置.我很确定控制器没有被释放,因为它在整个应用程序之后不久就被使用了.

关于我做错了什么的任何想法?

jon*_*oll 10

您的错误消息表明通知正在发送到您的MasterViewController类,而不是MasterViewController实例.您收到错误,因为receiveTestNotification:它是实例方法,而不是类方法.

我认为问题是你在initialize方法中注册通知,这是一个类方法,因此self在该上下文中引用类本身,而不是实例.

这是一个非常类似的先前问题,其中解决方案是在init方法中注册通知,这是一种实例方法.