unsorted
unsorted

Reputation: 3284

checking which UITextField is triggering textFieldShouldBeginEditing call

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.

Upvotes: 0

Views: 760

Answers (2)

tadejsv
tadejsv

Reputation: 2092

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

Upvotes: 0

RunLoop
RunLoop

Reputation: 20376

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

Upvotes: 3

Related Questions