简体   繁体   English

如何在Enter键上关闭键盘

[英]how can I dismiss keyboard on Enter keypress

I want to dismiss my keyboard as I press RETURN key. 当我按下RETURN键时,我想解雇我的键盘。

I have tried by putting button in view's back side. 我试过把按钮放在视图的背面。

But how can I do this by pressing RETURN key? 但是如何通过按RETURN键来完成此操作?

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

Don't forget to add the delegate UITextFieldDelegate 不要忘记添加委托UITextFieldDelegate

I hope you have done UIViewController <UITextFieldDelegate> and yourTextField.delegate=self ; 我希望你做过UIViewController <UITextFieldDelegate>yourTextField.delegate=self ;

and then in the delegate method 然后在委托方法中

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{

   [textField resignFirstResponder];
   return YES;

}

I'm presuming you're talking about a UITextField rather than a UITextView as your question isn't that clear? 我假设你在谈论UITextField而不是UITextView因为你的问题不是很明确吗? If so then ensure your class is marked as a UITextFieldDelegate in the interface file, 如果是这样,那么确保您的类在接口文件中标记为UITextFieldDelegate

@interface MyController: UIViewController <UITextFieldDelegate> {
   UITextField *activeTextField;

   // ...remainder of code not show ...
}

and then you should implement the two delegate methods as below, 然后你应该实现如下的两个委托方法,

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    activeTextField = textField;!    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    activeTextField = nil; 
    [textField resignFirstResponder];
    return YES;
}

However if you're using a UITextView then things are a bit more complicated. 但是,如果您使用的是UITextView那么事情就会复杂一些。 The UITextViewDelegate protocol lacks the equivalent to the textFieldShouldReturn: method, presumably since we shouldn't expect the Return key to be a signal that the user wishes to stop editing the text in a multi-line text entry dialog (after all, the user may want to insert line breaks by pressing Return). UITextViewDelegate协议缺少与textFieldShouldReturn:方法等效的方法,大概是因为我们不应期望Return键是用户希望在多行文本输入对话框中停止编辑文本的信号(毕竟,用户可能想按Return键插入换行符。

However, there are several ways around the inability of the UITextView to resign as first responder using the keyboard. 但是,有几种方法可以解决UITextView无法使用键盘作为第一响应者辞职的问题。 The usual method is to place a Done button in the navigation bar when the UITextView presents the pop-up keyboard. 通常的方法是在UITextView显示弹出键盘时在导航栏中放置一个完成按钮。 When tapped, this button asks the text view to resign as first responder, which will then dismiss the keyboard. 点击时,此按钮会要求文本视图作为第一响应者重新签名,然后将关闭键盘。

However, depending on how you've planned out your interface, you might want the UITextView to resign when the user taps outside the UITextView itself. 但是,根据您计划界面的方式,当用户点击UITextView本身时,您可能希望UITextView重新UITextView To do this, you'd subclass UIView to accept touches, and then instruct the text view to resign when the user taps outside the view itself. 为此,您将UIView子类UIView接受触摸,然后在用户点击视图本身之外时指示文本视图重新签名。

Create a new class, 创建一个新类,

#import <UIKit/UIKit.h>

@interface CustomView : UIView {
   IBOutlet UITextView *textView;
}

@end

Then, in the implementation, implement the touchesEnded:withEvent: method and ask the UITextView to resign as first responder. 然后,在实现中,实现touchesEnded:withEvent:方法并要求UITextView作为第一响应者辞职。

#import "CustomView.h"

@implementation CustomView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}

- (void) awakeFromNib {
    self.multipleTouchEnabled = YES;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"touches began count %d, %@", [touches count], touches);

    [textView resignFirstResponder];
    [self.nextResponder touchesEnded:touches withEvent:event];
}

@end

Once you've added the class, you need to save all your changes, then go into Interface Builder and click on your view. 添加完课程后,需要保存所有更改,然后进入Interface Builder并单击视图。 Open the Identity inspector in the Utility pabel and change the type of the view in your nib file to be your CustomView rather than the default UIView class. 在Utility pabel中打开Identity检查器,并将nib文件中的视图类型更改为CustomView而不是默认的UIView类。 Then in the Connections Inspector, drag the textView outlet to the UITextView . 然后在Connections Inspector中,将textView插座拖到UITextView After doing so, and once you rebuild your application, touches outside the active UI elements will now dismiss the keyboard. 执行此操作后,一旦重建应用程序,触摸活动UI元素外部现在将关闭键盘。 Note however that if the UIView you are subclassing is “behind” other UI elements, these elements will intercept the touches before they reach the UIView layer. 但请注意,如果你是子类的UIView是其他UI元素的“后面”,这些元素将在它们到达UIView层之前拦截触摸。 So while this solution is elegant, it can be used in only some situations. 因此,虽然这种解决方案很优雅,但它只能在某些情况下使用。 In many cases, you'll have to resort to the brute force method of adding a Done button to the navigation bar to dismiss the keyboard. 在许多情况下,您将不得不采用强制方法将“完成”按钮添加到导航栏以关闭键盘。

Make sure your view controller class is a delegate for your UITextField and then use the delegate method in that class: 确保您的视图控制器类是UITextField的委托,然后使用该类中的委托方法:

#pragma mark - Delegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    // Dismiss the keyboard when the Return key is pressed.
    [textField resignFirstResponder];

    return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM