views:

1253

answers:

2

How do I programmaticly assign a method (Observer?) to the "Did End On Exit" Event on a UITextField object?

This is easy enough to do in IB but I can't figure out how to do it in code...

+1  A: 

In your view controller implement the following method:

- (void)textFieldDidEndEditing:(UITextField *)textField{

//do stuff

}

Don't forget to set the delegate in viewDidLoad or the initializer:

myTextField.delegate = self;
Corey Floyd
This doesn't work...
Frank V
Uou have to make sure the delegate is set. Also the textField must resign first responder, which makes the keyboard disappear as a clue you have done it right.
Corey Floyd
What you posted maybe right, I don't know. However the code I posted above is more elegant and accomplishes exactly what I was looking for.
Frank V
I think what I meant to say was that UITextField was created with the delegate pattern in mind (as most Apple Controls are). Your implementation is equally valid. However, if you need to add custom code at different points in the UITextField's life cycle, it make sense to just implement the required delegate methods in your custom view controller instance. You can find all supported methods in the UITextFieldDelegate protocol reference.
Corey Floyd
+4  A: 

I just figured it out...

[mytextField addTarget:self 
  action:@selector(methodToFire:)
  forControlEvents:UIControlEventEditingDidEndOnExit];
Frank V