views:

876

answers:

3

In many situations, such as making the keyboard go away when the use clicks done, there are two options: set the text field's delegate to self, and adopt the UITextFieldDelegate protocol, and then use the method - (BOOL)textFieldShouldReturn:(UITextField *)textField; to resignFirstResponder and return YES. But you can also addTarget:self action:@selector(myMethod:) forControlEvent:UIControlEventDidEndOnExit]; or something like that, using the did end on exit event, and then in the method, [sender resignFirstResponder]. So what is the best option in situations like these: the delegate, or the event?

Thanks!!

A: 

The quick rule of thumb is that delegates are supposed to answer the question of "should I?" on behalf of the object they are a delegate for. Events, on the other hand, are broadcast afterward to let listeners know that something has happened.

In your case, while you could call [sender resignFirstResponder] in response to the event, you're mixing metaphors by doing this. Your delegate should have already made the decision to hide the keyboard (or not) and the event being broadcast is merely to let all the other components know that they keyboard hid.

Peter Nix
So in the case of hiding the keyboard it should use the delegate method to do [textField resignFirstResponder]; and to return YES?
Mk12
@Mk12 sounds about right to me
Peter Nix
I don't think that's a good rule of thumb, there are plenty of delegates built around informing (like the NSUrlConnectionDelegate methods) rather than asking permission.
Kendall Helmstetter Gelner
A: 

The delegate makes your objects more reusable they are an adapter that lets any object interact with the defined behaviors of that object and use the object. I would say delegates should be adopted by the object responsible for keeping the state of and defining behavior to actions that will occur in the object that it is using. Events should be used for any other objects that are intersted in particular action that the object that has the protocol does (so objects not responsible for keeping the state of the object that defines the protocol).

For example: A view controller using a textfield will adopt its protocol to dismiss the keyboard and any other behaviors that can occur for a textfield, maybe another controller will do some animation when the keyboard is dismissed, so it will register to the textfield as an event in order to receieve the event of the keyboard being dismissed.

Daniel
A: 

If you are going to be paired with one other class, where the real type of that class may vary, then it makes a lot of sense to formalize that pairing into a protocol and a delegate arrangement.

If the information you want to send is targeted at a broader set of objects, then it starts to make more sense to use notifications - though now you have somewhat obscured what information is being passed by the notification as there's no central definition for what to expect.

Both are about an equal load to work with - with a delegate you have to set yourself and then remember to unset yourself before you are deallocated. You have to do the same thing with notifications, remember to start listening and then unsubscribe before you are deallocated.

Also, you should try as much as possible to make sure you send notifications out on the main thread as the notices get sent on the same thread they started from. Same goes for delegate methods, it's not very kind to call a delegate method from some other mystery thread!

Kendall Helmstetter Gelner
what do you mean "with a delegate... and then remember to unset yourself" how do you "unset yourself", and where? And do you have to remove action listeners before the end of the program? I havn't been doing that.. could you explain how to? Thanks!
Mk12