简体   繁体   中英

iOS UITextField resignFirstResponder issue

In my app, on registration screen, I have tableView which containes UITextField in each cell and Submit button in a footer. Everything works fine except when I enter value in the last TextField and hit submit button (Without resigning a keyboard). My function does not show a value because value is only entered when text editing is ended. I am using Delegate to get a value.

My code is:
In customcell.m:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text length] >0) {
    [self.cellImageview setImage:editImage];
    }
    else{
    [self.cellImageview setImage:defaultImage];
    }
    [_delegate signUpTableCell:self didUpdateValue:textField.text inTextField:textField];
}

In my viewController:

-(void)signUpTableCell:(SignUpTableCell *)cell didUpdateValue:(NSString *)value inTextField:(UITextField *)textField    {
    NSLog(@"cell:%@    value: %@  textfield value:%@ ", cell, value, textField.text);
    [registerDictionary setValue:value forKey:textField.placeholder];
    NSLog(@"registerDictionary:%@", registerDictionary);
}

Problem:

Last textField value is nil when I click on submit button, probably because -(void)signUpTableCell:(SignUpTableCell *)cell didUpdateValue:(NSString *)value inTextField:(UITextField *)textField does not get a call unless textField ends editing. If I click in empty place and resign a keyboard it works just fine. Is there a work around this problem?

单击按钮时是否尝试手动调用resignFirstResponder()

Add below code to in submit button :

[self.view endEditing:yes];

or use

[self.yourTableview.scrollView endEditing:yes];

Fixed it by making few changes in my code. I had used [self.view endEditing:yes]; after I get values from registerDictionary . Stupid of me.

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