views:

49

answers:

2

What's the best way to determine which UITextField triggers the method -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField (or any of the other UITextFieldDelegate methods)? I've seen code like this before:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    if (textField == textFieldCode) {
        return YES;
    }
    return NO;
}

but this only works if I have textFieldCode as an ivar in my class, and in this case I'm just initializing a couple of UITextFields and putting them in a table, so I don't have references to them in the class.

I was thinking that I could use the hash function and store the hashes for each textField somewhere in the class, and then compare textField's hash to the desired hash in the method call, but that seems like kind of a hack.

+3  A: 

Since you only have a couple of fields, you can assign unique numbers to the tag properties of each textfield to enable identification.

Run Loop
perfect, that's just what I needed. also, tip for future SOers: add enum field to header file to make this more robust.
unsorted
If your code gets too messy you can also factor out your code to separate helper objects that implement the textFieldDelegate protocol. One helper object for each field that the controller creates and assigns as the delegate. You can use notifications to update the controller with changes
falconcreek
A: 

You can have an iVar of NSArray that will contain all the text fields. Then just enumerate through it to find out which text field sent the message

tadej5553