隐藏按钮

时间:2015-06-01 07:48:50

标签: ios objective-c

我想要监视IOS上的键盘隐藏按钮并在其上触发事件。我在谈论:

ipad keyboard

我想监视,然后用户按下实际按钮。键盘隐藏时我不想要这个事件。

4 个答案:

答案 0 :(得分:1)

用户键盘通知观察员..

对于Swift

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillAppear"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide"), name: UIKeyboardWillHideNotification, object: nil)


func keyboardWillAppear() {
    println("Keyboard appeared")
}

func keyboardWillHide() {
    println("Keyboard hidden")
}

对于Objective-C

[[NSNotificationCenter defaultCenter] addObserver:self 
                                     selector:@selector(keyboardWillShow:) 
                                         name:UIKeyboardWillShowNotification 
                                       object:nil];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self 
                                     selector:@selector(keyboardWillHide:) 
                                         name:UIKeyboardWillHideNotification 
                                       object:nil];

答案 1 :(得分:0)

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

-(void)keyboardWillHide:(NSNotification *)notif
{
//keyboard will hide
}

UIKeyboardDidHideNotification也可以使用。

如果您没有任何其他触发器来隐藏键盘(例如在外面点击或在返回时隐藏),您可以确保由“键盘隐藏”按钮触发的事件

答案 2 :(得分:0)

请查看以下内容 -

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (text.length != 0) {
        // Get the Ascii character
        int asciiCode = [text characterAtIndex:0];

        if (asciiCode == 10) {

            // your keyboard hide button is press

        }
    }
}

答案 3 :(得分:0)

出于奇怪的原因,当用户按下 hide keyboard button 时,会触发事件 UIResponder.keyboardWillShowNotification

这是我发现知道用户是否按下 hide keyboard button 的唯一方法:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillAppear(_:)), name: Notification.Name(rawValue: UIResponder.keyboardWillShowNotification.rawValue), object: nil)
@objc func keyboardWillAppear(_ notification: Notification) {
    if let info: [AnyHashable: Any] = (notification as NSNotification).userInfo {
        if let keyboardCGRect = (info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
           keyboardCGRect.height > 100 {
           // Keyboard is presented
        } else {
           // User pressed the hide button.
        }
    }
}