NotABot
NotABot

Reputation: 526

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?

Upvotes: 1

Views: 382

Answers (3)

NotABot
NotABot

Reputation: 526

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.

Upvotes: 0

Nidhi Patel
Nidhi Patel

Reputation: 1292

Add below code to in submit button :

[self.view endEditing:yes];

or use

[self.yourTableview.scrollView endEditing:yes];

Upvotes: 1

Stefan Stefanov
Stefan Stefanov

Reputation: 829

Did you try to manually call resignFirstResponder() when clicking on the button?

Upvotes: 1

Related Questions