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:
.