Search code examples
iphonecocoa-touchnsnotificationsaddobserver

How to specify notificationSender? (Cocoa Touch)


All of the examples I can find of setting up a notification look like this:

[nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];

where object is always being set to nil. object is the notificationSender, which is defined thusly in the documentation:

"The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. When nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer."

I want to receive the keyboard notification only for one particular text view, so I want to tell addObserver that, but I don't know what to give it for the object. I tried the outlet variable that's bound to the text view I'm interested in, but that just resulted in my getting no notifications at all.

Any suggestions?


Solution

  • The UIKeyboardWillShowNotification is coming from your window instance, and for all intents and purposes is a "system" notification. The keyboard is either showing or not showing, it's not really something that is tied to a specific control.

    If you want to do something when a user enters a specific text field, you should probably control that in the text field's delegate instead.