0

I am adding text fields to a view programmatically like this:

// Add a text field.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 280, 40)];
textField.returnKeyType = UIReturnKeyNext;
[textField becomeFirstResponder];
[textField addTarget:self action:@selector(nextButtonPressed:) forControlEvents:UIControlEventEditingDidEndOnExit];

UITextField *textFieldTwo = [[UITextField alloc] initWithFrame:CGRectMake(20, 160, 280, 40)];
textFieldTwo.returnKeyType = UIReturnKeyDone;
[textFieldTwo addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventEditingDidEndOnExit];

How can I select these fields later on?
I know how to do this when I'm creating things using UI, but how does this work for dynamically added elements?

Example: I want to focus the second field when "Next" button is pressed.

Thanks!

1
  • you can add a tag for every UITextField and in the event : textFieldDidEndEditing:, focus the next one if the previous one has been filled. You can as well, check if the return key has been played in the keyboard to go next. This is a way to tackle that, but there are more. In the approach you have introduced, you can do it in that way. In the event, you can see the tag of the UITextField, so you can navigate or focus the next one. Commented Jul 22, 2014 at 14:15

4 Answers 4

2

There are at least two ways of doing it:

  • Give each of your dynamically created fields a distinct tag, and then retrieve the required field using the tag that you gave it by calling viewWithTag: on the view to which you added your fields, or
  • Make textField and textFieldTwo instance variables of your class, initialize them when you have to, and then refer to these ivars later when you want to send input to them.

The second way is close to what you do when you add the fields through the Interface Builder. The only difference is that in this case the fields are added programmatically.

Sign up to request clarification or add additional context in comments.

Comments

1

In this case, I usually set a tag for each textfield present on my viewController, and assign them my viewController as delegate :

// You  should use const to identify quickly your tag
textField.tag          = 10;
textFieldTwo.tag       = 11;
textField.delegate     = self;
textFieldTwo.delegate  = self;

Then I implements the textFieldShouldReturndelegate method :

#pragma mark - UITextFieldDelegate protocol conformance

-(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.
}

By doing so, you can have multiple UITextField with a focus moved from one to another without having to implements multiple UIControlEventEditingDidEndOnExit event methods.

Comments

1

You can get these by there tag value. For this

1.. set a unique tag value, to each textField at time of creation. (like 45 for first, and 78 for second textfield)

 textField.tag = 45; 

2.. suppose your you have added these textField as subView on 'myView'.

  UITextField *txtField = (UITextField*)[myView viewWithTag:45];

this line will give you textfield having tag 45, which is added on myView.

Note -- Avoid to use '0' as tagValue for any control because '0' is used as byDefault tagValue for controls.

Comments

0

On UIControlEventEditingDidEndOnExit event for the first textField, call [textFieldTwo becomeFirstResponder].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.