twp
twp

Reputation: 31

NSNotification for single object in swift

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?

Upvotes: 3

Views: 626

Answers (1)

Avba
Avba

Reputation: 15266

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

Upvotes: 1

Related Questions