AlexaH
AlexaH

Reputation: 41

When Will textFieldDidEndEditing:textField be called?

I have 2 UITextField in a ViewController. One brings up a UIDatePicker another brings up a KeyBoard(number pad). I then assign user inputs to two class variables, a NSDate variable and a NSNumber variable, in the following method.

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == self.datePickerTextField){
        //update xxtime
        self.xxTime = self.datePicker.date;
        NSLog(@"...%@",self.xxTime);
    }
     if (textField == self.numberTextField) {
        int val = [textField.text intValue];
        self.xxNumber = [NSNumber numberWithInt:val];
        NSLog(@"...%@",self.xxNumber);
    }

}

If I tap the datePickerTextField first then the numberTextField, this method won't get called after I finish typing in the numberTextField.

So my question is how do I make this method to get called? Should I specify "resignFirstResponder" somewhere?

TIA

Upvotes: 4

Views: 3996

Answers (3)

Jwlyan
Jwlyan

Reputation: 84

Please check what iOS you are using. Remember that if you are using iOS 10, there is a method that is replacing the old textFieldDidEndEditing called func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason)

Upvotes: 1

LML
LML

Reputation: 1667

Yo are right you need to specify resignFirstResponder

- (BOOL)textFieldShouldReturn:(UITextField *)textField {//Text Field Delegate
    if (textField == textField1) {
        [textField1 becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
    }
    return TRUE; }

this can used with textFieldDidEndEditing also

Upvotes: 2

Shai
Shai

Reputation: 25619

It will invoke didEndEditing once another control gains focus

According to the API,

This method is called after the text field resigns its first responder status.

So if you want didEndEditing to be invoked, you need to call

[textField resignFirstResponder];

Upvotes: 8

Related Questions