This question already has answers here:
who is issuing my UIKeyboardDidHideNotification?
                                
                                    (2个答案)
                                
                        
                                5年前关闭。
            
                    
键盘出现后,我需要在UIScrollView中移动UI元素,以便用户可以看到它。

若要实现此行为,我调用以下方法:

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)deregisterFromKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardDidHideNotification
                                                  object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
}


viewWillAppear:viewWillDisappear:方法中。

但是,在我的情况下,同一视图控制器上有两个文本视图,但是我只需要移动UI元素(仅其中之一)。我该怎么做?我可以以某种方式仅对一个对象调用addObserver还是检查哪个对象调用了它?

提前致谢。

最佳答案

您可以询问文本视图是否具有键盘焦点:

if ([myTextView1 isFirstResponder]) {
    // Do this
} else if ([myTextView2 isFirstResponder]) {
    // Do that.
}

关于ios - 如何知道哪个对象发送了通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24992181/

10-10 10:01