简体   繁体   English

在动态UITableView中关闭iPhone键盘

[英]dismiss iPhone keyPad in dynamic UITableView

I have UITableView with two sections. 我有两个部分的UITableView First section is a static row with stepper which creates cells of second section. 第一部分是带有步进器的静态行,该行创建第二部分的单元格。 Each cell of second section contains the UITextField with keypad. 第二部分的每个单元格都包含带有键盘的UITextField I can dismiss the keypad using UITapGestureRecognizer or additional DONE button in keypad but it is working for the last cell only. 我可以使用UITapGestureRecognizer或键盘中的其他“完成”按钮关闭键盘,但是它仅适用于最后一个单元格。 I have tried the following methods: 我尝试了以下方法:

  1. UITapGestureRecognizer in the table view 表格视图中的UITapGestureRecognizer

in my ViewDidLoad I put: 在我的ViewDidLoad

   UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyboard)];
    [self.tableView addGestureRecognizer:gestureRecognizer];

and then 接着

-(void)hideKeyboard{
    [self.tableView resignFirstResponder];
}

or 要么

-(void)hideKeyboard:(UITapGestureRecognizer*)sender{
    [self.cellText endEditing:YES];
}
  1. Use tags to recognize which textfield I am editing. 使用标签来识别我正在编辑的文本字段。

In

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath I added
    [cellText addTarget:self action:@selector(myNumberValueBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];

and in 和在

-(void)myNumberValueBeginEditing:(UITextField *)sender
{
    int row = [sender.superview.superview tag];
    UITextField *cellTemp = (UITextField*)[(UITableViewCell *)sender.superview viewWithTag:200+row];
    cellTemp.delegate = self;
    [cellTemp becomeFirstResponder];
}

then I am trying to resignFirstResponder in DONE button 然后我想在“完成”按钮中退出FirstResponder

-(IBAction)doneButton:(UITextField *)sender {
NSLog(@"doneButton");
    int row = [sender.superview.superview tag];
    [(UITextField*)[(UITableViewCell *)sender.superview viewWithTag:200+row] resignFirstResponder];
}

I have no more ideas how to resign the keypad from all UITextFields . 我没有更多的想法如何从所有UITextFields退出键盘。 If anyone has some remedy, I will really appreciate. 如果有人可以补救,我将不胜感激。

You were close with your use of endEditing: . 您对endEditing:的使用非常接近。 Instead of sending endEditing: to an instance of your text field, try sending it to your main view. 与其发送endEditing:到文本字段的实例, endEditing:尝试将其发送到主视图。 ex: 例如:

[self.view endEditing:YES];

endEditing: can be sent directly to a text field instance, or to a view, in the case of the latter any text field that is editing that is a subview of the view you specified will resign first responder. endEditing:可以直接发送到文本字段实例,也可以发送到视图,如果是后者,则任何正在编辑的文本字段(即您指定的视图的子视图)都将退出第一响应者。

From the docs: 从文档:

Causes the view (or one of its embedded text fields) to resign the first responder status 使视图(或其嵌入式文本字段之一)退出第一响应者状态

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

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