简体   繁体   English

当用户在textField外部点击时关闭键盘

[英]Dismiss keyboard when user taps outside the textField

I'm trying to dismiss the keyboard when the user selects something other than the textFiled . 当用户选择除textFiled之外的其他内容时,我试图关闭键盘。 I tried the following: 我尝试了以下方法:

- (void)viewDidLoad {
    mytextField1.delegate = self;
    mytextFiled2.delegate = self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];    
}

This didn't work. 这没用。 I then tried [... resignFirstResponder] in touchesBagan , and that didn't work either. 然后,我在touchesBagan尝试了[... resignFirstResponder] ,但该方法也不起作用。 Am I doing something wrong? 难道我做错了什么? Why is this not working? 为什么这不起作用?

您是否尝试过在文本字段的超级视图上添加UITapGestureRecognizer以关闭键盘?

Add a tap gesture to the view (not the text field) so that you know when someone taps the view. 向视图(而不是文本字段)添加轻击手势,以便您知道某人何时单击视图。

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped)];
[self.view addGestureRecognizer:tap];

Then when that method is called, resign first responder to dismiss the keyboard. 然后,在调用该方法时,请辞职第一响应者以退出键盘。

- (void)viewTapped
{
    if (self.textField.isFirstResponder) {
        [self.textField resignFirstResponder];
    }
}

You can use UITapGestureRecognizer. 您可以使用UITapGestureRecognizer。 And also save the current focused TextField at activeTF. 并将当前已聚焦的TextField保存在activeTF中。 You need to set the current view controller as the delegate of UITextField(s). 您需要将当前视图控制器设置为UITextField的委托。

UITextField *activeTF;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    .......

    UITapGestureRecognizer *dismissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:dismissTap];
}


- (void)dismissKeyboard {
    if (activeTF) {
        [activeTF resignFirstResponder];
    }
}

#pragma mark - TextField Delegate

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    activeTF = sender;
}

- (void)textFieldDidEndEditing:(UITextField *)sender
{
    activeTF = nil;
}

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

相关问题 当用户点击文本字段外的其他区域时如何关闭键盘? - How to dismiss keyboard when user tap other area outside textfield? 在文本框外触摸时如何关闭键盘? - How to dismiss keyboard when touch outside the textfield? 当用户点击其他textField时,关闭pickerView并打开相应的pickerView - Dismiss pickerView and open the corresponding one when a user taps on a different textField 当用户点击视图外的任何地方时关闭视图 - Dismiss a view when a user taps anywhere outside the view 如果用户双击文本框,键盘将变黑 - Keyboard goes black if user double taps textfield 当用户触发操作表并将其关闭时,如何在文本字段上关闭键盘? - How to dismiss keyboard on a textfield when a user triggers an action sheet and dismisses it? 仅当用户点击UITableView的某些区域时,需要关闭显示的键盘 - Need to dismiss keyboard which appears, when user taps on certain areas of UITableView only 没有文本字段要退出时如何关闭键盘 - how to dismiss a keyboard when there is no textfield to resignFirstResponder 当文本字段没有IBOutlet时关闭键盘 - dismiss keyboard when the textfield does not have IBOutlet 用户点击键盘时输出特定图像 - Output specific images when user taps the keyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM