-2

Let me explain, I have a UIViewController in a thread n.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myCommandNotification:) name:NetConnection object:nil];

I have an other Class in thread n +1 and i post a message.

[[NSNotificationCenter defaultCenter] postNotificationName:myCommandNotification: object:MyObject];

Ok so far no problem I'm happy lol.

Well, yes there is problem for later.

now when i try to use

SEL selector = NSSelectorFromString([NSString stringWithFormat:@"%@:", myCommandNotification]); 

if ([[NSNotificationCenter defaultCenter] respondsToSelector:selector])

The return code is always NO.

When i use

if ([MyViewController self] respondsToSelector:selector])

it's work

I'm stupid or not ?!!

Many Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alan10977
  • 973
  • 1
  • 9
  • 7

2 Answers2

0

Thats because the notification centre doesn't have this method, only the view controller does. The notification centre is just a singleton that link objects (like view controllers) to a specific notifications. It doesn't respond to the selector, only the object themselves.

Avi Tsadok
  • 1,843
  • 13
  • 19
  • Imaging the notification centre is an object that has an array/dictionary of objects and selectors linked to a notification name. When you tell the notification centre to POST notification (by name), it search this list, and when it finds an object that linked to this notification name, it fires it's method. Thats it. Is that clear? – Avi Tsadok Dec 19 '14 at 20:17
  • Responses to selector means the object has this method and can execute it. In this case the notification centre doesn't have this method, It just knows what objects listen to this notification and execute THIER methods. Thats it – Avi Tsadok Dec 19 '14 at 20:18
  • Post 1 ok for me, but the point 2 i don't really a response for SEL selector = NSSelectorFromString([NSString stringWithFormat:@"%@:", myCommandNotification]); if ([[NSNotificationCenter defaultCenter] respondsToSelector:selector] not work – Alan10977 Dec 19 '14 at 20:35
  • If i understod defaultcenter retain the name and not the selector it' t right ? – Alan10977 Dec 19 '14 at 21:04
  • I use UIViewController self for my ResponseToSelector and that work without problem. Many tanks. I understood now, it's impossible with defaultcenter to have with their design. – Alan10977 Dec 20 '14 at 15:21
  • If you understand, you can always mark my answe... :) – Avi Tsadok Dec 20 '14 at 19:49
0

You're confused. It won't be the notification center that responds to your myCommandNotification: selector, it will be the notification observer. That's the object the notification center will send the message to when it detects a notification that matches.

This line:

if ([[NSNotificationCenter defaultCenter] respondsToSelector:selector])

Makes no sense.

Next problem: You set up your notification to call the selector myCommandNotification: for a notification with the name NetConnection (whatever that is. Shouldn't begin with an upper-case letter, but let's ignore that.)

Next, you post a notification the the name myCommandNotification. You created your observer to listen for a notification with the name NetConnection, so unless myCommandNotification and NetConnection are both strings containing the same value, you're not going to trigger your notification handler.

Let's say you add an observer like this (using string constant for clarity)

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

Then to post a notification that that observer would respond to, it would look like this:

[[NSNotificationCenter defaultCenter] postNotificationName: @"aNotice" 
  object: self];

Note that the notification NAME is the same string in both cases. If you supply a specific object when you add an observer you will only be called for notifications who's object parameter matches the object you specified in your call to addObserver:selector:name:object:. I my example I added an observer but provided a nil object parameter, so my observer will get called regardless of the object specified in the call to postNotificationName:object:.

Duncan C
  • 128,072
  • 22
  • 173
  • 272