Swift 2 addObserver for a specific textField with an object parameter

As far as I understand, the objectmethod parameter addObserveris the object from which you want to receive notifications. Most of the time I see it as nil(I assume that this is due to the fact that notifications about the specified type are required from all objects). In my specific case, I have a text box at the top of the screen and at the bottom of the screen, and I want the view to only move when the user deletes the bottom text box, not the top one. Therefore, I call the following method inviewWillAppear

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

selector keyboardWillShow:and call methods keyboardWillHide:that reposition the presentation frame. I tried to leave the parameter objectequal nil, but would receive notifications from both text fields. I tried setting the parameter objectto self.bottomTextField(as shown above), but it does not receive notifications from either the text box. My question is twofold. Firstly, I correctly understand the method addObserver(especially the parameter object), and secondly, why it does not register notifications from self.bottomTextField. Thanks!

Solution: I know that this is not a solution to the exact question I asked, but what I did at the end to make it look up when I just click on the lower text box was the following:

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

and then in the method keyboardWillShow:I have:

func keyboardWillShow(notification: NSNotification) {
    if bottomTextField.editing { // only reset frame origin if editing from the bottomTextField
        view.frame.origin.y -= getKeyboardHeight(notification)
    }
}

Hope this helps!

+4
2

-, addObserver ( )

, . nil , , ; , .

second, self.bottomTextField

. UITextField UIKeyboardWillShowNotification - , . nil object, , , . , , . UITextFieldTextDidBeginEditingNotification UITextFieldTextDidEndEditingNotification, , UITextField.

+3

self.bottomTextField , , u , , , , nil ,

, , , textFieldShouldBeginEditing textFieldShouldEndEditing, .

:

#import "UIResponder+FirstResponder.h"

static __weak id currentFirstResponder;

@implementation UIResponder (FirstResponder)

+(id)currentFirstResponder {
    currentFirstResponder = nil;
    [[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:) to:nil from:nil forEvent:nil];
    return currentFirstResponder;
}

-(void)findFirstResponder:(id)sender {
    currentFirstResponder = self;
}
+1

Source: https://habr.com/ru/post/1618541/


All Articles