HardCode
HardCode

Reputation: 2025

How do I call didEndEditing?

I'm doing form validation on my iPhone app. I want to call the didEndEditing method on my button submit. How do I do this?

Here is my code

This is the method

-(void)didEndEditing:(UITextField*)textField;
{
   // Do Something!
}

Here I'm calling the method

-(IBAction)btnSelected:(id)sender {
   UIBarButtonItem *bbi = (UIBarButtonItem *) sender;

   if (bbi.tag == 1) {

       [self didEndEditing]; // How do I call this method?
    }
 }

Can someone help me?

Upvotes: 0

Views: 2346

Answers (2)

Mattias Wadman
Mattias Wadman

Reputation: 11425

I assume your refering to textViewDidEndEditing:? you should not call that method yourself it will be called when the text field is not the first responder anymore. From the API documentation:

Implementation of this method is optional. A text view sends this message to its delegate after it closes out any pending edits and resigns its first responder status. You can use this method to tear down any data structures or change any state information that you set when editing began.

If you want validate I suggest that you refactor out the validation code and call it from both textViewDidEndEditing: and submit.

You can loop thru all text fields like this:

for (UITextField *textField in [NSArray arrayWithObjects
                                self.textField1,
                                self.textField2,
                                nil]) {
   // validate textField
}

If you need different validation per field you could make a more complex data structure that includes validation block, regex etc for each field.

Upvotes: 2

Dancreek
Dancreek

Reputation: 9544

You should just resignFirstResponder on the active textField:

-(IBAction)btnSelected:(id)sender {
   UIBarButtonItem *bbi = (UIBarButtonItem *) sender;

   if (bbi.tag == 1) {

       [self.myActiveTextField resignFirstResponder];
    }
 }

Then didEndEditing should get called on its own.

Upvotes: 0

Related Questions