質問

Is there a way to detect if a UITextField exists using the Tag property? Essentially i have a number of textfields created dynamically and I want to tab through the fields using the return key on the keypad.

I am trying to use the below code form another post but 'textField.superview' returns null. I am creating the textfields programatically.

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
  NSInteger nextTag = textField.tag + 1;
  // Try to find next responder
  UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
  if (nextResponder) {
    // Found next responder, so set it.
    [nextResponder becomeFirstResponder];
  } else {
    // Not found, so remove keyboard.
     [textField resignFirstResponder];
  }
  return NO; // We do not want UITextField to insert line-breaks.
}
役に立ちましたか?

解決

You can directly check like this

       -(BOOL)textFieldShouldReturn:(UITextField*)textField {
             if (textField.tag == 1) {
             //Do the stuff what you want here
             }
        }

For setting the tag

        textFieldName.tag = 1;

他のヒント

You can use isKindofClass: method.You can do like this

 if ([[textField.superview viewWithTag:nextTag] isKindOfClass:[UITextField class]])
  {
    // Found next responder, so set it.
    [(UITextField *)[textField.superview viewWithTag:nextTag] becomeFirstResponder];
  } else {
    // Not found, so remove keyboard.
     [textField resignFirstResponder];
  }
  return NO; // We do not want UITextField to insert line-breaks.
}

Did you set the Tag of the text fields? The tag field is not set automatically, you have to set it yourself when you create the text field so you can identify it later. If you don't set it, they all default to 0 - so you will never find any field with a text > 0.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top