Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare the instance of UITextField

Tags:

iphone

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!

like image 780
xueru Avatar asked Jan 23 '26 00:01

xueru


2 Answers

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".

like image 159
kennytm Avatar answered Jan 24 '26 19:01

kennytm


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
    } ...
like image 37
progrmr Avatar answered Jan 24 '26 19:01

progrmr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!