开发者

How to create an NSNotification

I thought this would be very easy but is not. I searched here in stackOverflow and didn't find an answer. I only want to create a NSNotification like a variable.

I already configured the listener and have a valid function. So if I send:

[[NSNotificationCenter defaultCenter] postNotificationName:@"name" object:nil];

This works with no problems. But, if I try it in the same place with:

NSNotification *test = [NSNotification notificationWithName:@"name" object:nil];
[[NSNotificationCenter defaultCenter] postNotification:test];

It doesn't work. What am I doing wrong?

Edit

This error appear in XCode:

 -[object function]: unrecognized selector sent to instance 0x190d90
 *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[object function]: unrecognized selector sent to instance 0x190d90'

Edit 2

Was a bug. I don't believe I start this question. There are a very small error in @"name", they are different in my code, like @"nname" and @"name".

Because this I learn 2 things:

  • Try NEVER use postNotifi开发者_开发技巧cation. Only when there is no other way. This is difficult to debug and can cause error.
  • Always use only one variable when have to write it a lot of time, like #define NAME @"name" will save a lot of time to me.


It sounds to me like the problem is not in the way you are posting the notification, but rather in the way you are listening to it. You've probably told the listening class to call a selector that doesn't exist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜