I have 3 UITextFileds on a view, and I want to apply logic in UITextFieldDelegate method only in two of them, how do I determine the UITextField that triggered the call backs?
Many thanks in Advance!
Usually, simple pointer comparison works, since you just want to check for object identity.
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
if (textField != theIgnoredTextField) {
...
Alternatively, you could assign .tags to the text field.
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
if (textField.tag != 37) {
...
The advantage is you don't need to store the reference to theIgnoredTextField, and the tag can be set from Interface Builder, but it relies on a recognizing magic number "37".
The delegate methods have a textfield parameter which is a point to the textfield object. You can compare that parameter to your textfield objects to see which one it is.
UITextField *field1, *field2, *field3;
In your delegate method you can compare the parameter:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == field1) {
// do something special for field 1
} ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With