简体   繁体   中英

dismiss iPhone keyPad in dynamic UITableView

I have UITableView with two sections. First section is a static row with stepper which creates cells of second section. Each cell of second section contains the UITextField with keypad. I can dismiss the keypad using UITapGestureRecognizer or additional DONE button in keypad but it is working for the last cell only. I have tried the following methods:

  1. UITapGestureRecognizer in the table view

in my ViewDidLoad I put:

   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

-(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 . If anyone has some remedy, I will really appreciate.

You were close with your use of endEditing: . Instead of sending endEditing: to an instance of your text field, try sending it to your main view. 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.

From the docs:

Causes the view (or one of its embedded text fields) to resign the first responder status

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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