Search code examples
iosuitextfielduitextfielddelegate

Delay in UITextFieldDelegate event


This is a strange problem but I am perplexed on how to solve this - I have a UITableView that has custom UITableViewCells. Each UITableViewCell has two UITextFields and each UITextField is linked to a delegate that processes the textFieldDidEndEditing event. This works perfectly except in one instance.

Problem

The screen also has a 'Save' button and the problem arises when the user edits a UITextField and directly clicks the 'Save' button without clicking elsewhere in the screen. In such an event, the saveAction method is invoked before the textFieldDidEndEditing event and as a result the last edit of the user is lost.

I tried to debug using NSLog statements and found that while the textFieldDidEndEditing is indeed getting called, it is called after the saveAction event.

I thought about calling the textFieldDidEndEditing event from saveAction but that didnt make sense as I would have no idea about which UITextField is being edited.

Any suggestions are very much appreciated.


Solution

  • you could make a note of the text field that is active when the –textFieldDidBeginEditing: delegate method is called in your view controller

    have an assigned property that points to the active text field and then in -saveAction send it -resignFirstResponder.

    header:

    @property (nonatomic, assign) UITextField * editingTextField;
    

    m file:

    -(void)textFieldDidBeginEditing:(UITextField *)textField{
    self.editingTextField = textField;
    }
    
    -saveAction{
    if(self.editingTextField)
        [self.editingTextField resignFirstResponder];
    
     //continue implementation
    }