I strongly prefer using the target/action pattern by responding to the UIControlEventDidEndOnExit
event, usually by wiring it up in Interface Builder by connecting the "Did End On Exit" event shown in IB to the File's Owner, using the method of my choice. This would be the first option you showed.
Here's why I prefer this mechanism:
It many apps (well, at least my apps), it is necessary to distinguish between canceling input, say, by touching outside the text field, and completion of input by the done button on the keyboard (the docs tend to refer to this as the "return" button). Because the -textFieldDidEndEditing
delegate method gets called any time -resignFirstResponder
is invoked when the field is first responder (regardless of the reason for ending editing), it is necessary to have a variable somewhere to track the path of how you're terminating editing. This introduces a level of complexity that simply isn't necessary.
In my app, I respond to touch events outside the UITextField
, invoking -resignFirstResponder
to cancel editing without taking further action. If I use the delegate methods, I would need to set state here to record that I'm taking the 'cancel' path through my code, and use the -textFieldShouldReturn
delegate method to both set state to indicate I'm going through the 'done' path of my code, and invoke -resignFirstResponder
. Messy.
Using the target/action pattern here leads to simpler, cleaner code.