美文网首页
iOS 中NSNotificationCenter的回调函数不执

iOS 中NSNotificationCenter的回调函数不执

作者: playboy | 来源:发表于2017-12-08 14:15 被阅读0次

今天发现在项目中抛出NSNotificationCenter的事件,一直执行不到回调函数。经过一番排查,发现原来是在注册时添加了Object参数,发送时,却没有填充Object参数。被坑了良久,在次记录一下。

代码简略如下:

@implementation TWGroupChatViewController

- (void)viewDidLoad

{

[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:"NOTIFICATION_TEST" object:self];

}

-(void)test:(NSNotification*)notification

{

}

@end

在其他地方调用的代码:

[[NSNotificationCenter defaultCenter] postNotificationName:"NOTIFICATION_TEST" object:nil userInfo:nil];

问题所在:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:"NOTIFICATION_TEST" object:self];这里的object参数传入了self,但是发出通知的地方,object参数却传入了nil,两者不一致,导致注册的函数不被调用。

改正方法:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:"NOTIFICATION_TEST" object:nil];

添加事件时,将object参数传入nil即可。

相关文章

  • iOS 中NSNotificationCenter的回调函数不执

    今天发现在项目中抛出NSNotificationCenter的事件,一直执行不到回调函数。经过一番排查,发现原来是...

  • iOS 多播委托

    iOS回调有Delegate,NSNotificationCenter,block,kvo。但是项目中回调 有种情...

  • 通知、代理、block

    通知、代理、block 是iOS中的三大回调方式。 通知 NSNotificationCenter,它就像一个广播...

  • 通知、代理、block

    通知、代理、block 是iOS中的三大回调方式。 通知 NSNotificationCenter,它就像一个广播...

  • 回调地狱

    如果你想阅读体验更好,可以戳链接回调地狱 前言 从前一文中你真的了解回调我们已知道回调函数是必须得依赖另一个函数执...

  • apply和call语句

    运动框架的应用实例 结合 异步和回调函数 中的 「回调函数2」我们可以试图在回调函数中,用this表示oDiv对象...

  • javascript高级-

    函数类型中,比较常用的是匿名函数和回调函数,一般来说,回调函数是以匿名函数的形式来进行表现的。回调函数在事件监听,...

  • JavaScript函数_08回调函数

    回调函数 回调函数(回调),当我们把某个函数作为参数传递给另一个函数的时候,这个函数就是回调函数 回调函数的基本写...

  • python之回调函数和装饰函数

    一.回调函数 1.回调函数的概念: 是在某一函数中调用另一个函数变量方式,来执行函数.回调函数不是有实现方调用,...

  • 通知

    iOS中通知的使用NSNotificationCenter

网友评论

      本文标题:iOS 中NSNotificationCenter的回调函数不执

      本文链接:https://www.haomeiwen.com/subject/sreeixtx.html