3

I have two text fields, topTextField and bottomTextField, at the top and bottom of the screen, respectively. bottomTextField hides behind the virtual keyboard when it comes up so to fix that I'm using NSNotification to listen for the virtual keyboard and sliding the view up when that happens. However, the view slides up whenever the keyboard comes onto the screen, including when topTextField becomes first responder, effectively moving it off the screen. Here is the code:

    func subscribeToKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: bottomTextField)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: bottomTextField)
}

func unsubscribeToKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: bottomTextField)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: bottomTextField)
}

I originally had "nil" as the object parameter at the end of the methods and this is when I started looking for a way to isolate the behavior to just bottomTextField.

I am assuming that the key to solving this problem is in the object parameter in these methods, where I am currently passing in bottomTextField. Apple's documentation for NSNotificationCenter says that parameter is for

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.

If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

I'm interpreting that to mean that we can call out a specific object to listen for, so in this case bottomTextField. However when I changed those from "nil" to "bottomTextField" the view no longer slides up and bottomTextField is still hidden. Am I interpreting this incorrectly? If not, what can I do to get this to work? And if I am incorrect, is there a way to isolate a single object to listen for with NSNotification?

Community
  • 1
  • 1
twp
  • 31
  • 3

1 Answers1

1

The object should be nil. Not the textfield

In the handler check which text fields are "covered" by the keyboard

-(void)keyboardHandler:(NSNotufication *)note {

Cgrect keyboardFrame = [note.userInfo[keyboardframe] cgrectvalue]; // dont remember the key name (answering from my phone :)). Check uiapplication header. 

// check which text field is covered and move it

// you may need to convert the rect coordinates using:

cgrect windowFrame = [[[uiapplication sharedapplication] keyWindow] convertRect:textfieldFrame fromView:textfield.superView]];

If (windowFrame is covered by keyboard ...) { 
    Move text field....
}

hope its clear enough....

Avba
  • 14,822
  • 20
  • 92
  • 192
  • Thank you for your response.I modified my code to check for which text field was the first responder and move the keyboard if it was the bottom one. Works great now. I'm still a little fuzzy on what the notificationSender (the object) parameter would be used for if not something like this (I'm assuming that it didn't work because it wasn't the text field that was sending the notification). What kinds of things send notifications? In what instances might I use that last parameter as anything but nil? – twp Sep 02 '15 at 14:54
  • In cases where many objects at runtime could emit the same "notification name" and you would only want to track notifications from a "specific" object (It would make sense to specify the instance when enrolling). In cases where the system emits notifications, usually there is no added value to enroll to a specific object (many times it is a singleton and the instance is clear). Also in some cases the emitter of the notification doesn't attach enough context in the NSNotification Object (maybe the notification.object == nil?) but you need the instance which posted – Avba Sep 02 '15 at 15:02
  • For those that don't know how to check which text field is first responder, check [this stack overflow](http://stackoverflow.com/questions/12173802/trying-to-find-which-text-field-is-active-ios). – user3731622 Feb 07 '16 at 19:42