Aram Boyajyan
Aram Boyajyan

Reputation: 844

How to select programmatically created text fields?

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!

Upvotes: 0

Views: 176

Answers (4)

Michaël Azevedo
Michaël Azevedo

Reputation: 3894

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.

Upvotes: 1

Yogendra
Yogendra

Reputation: 1716

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.

Upvotes: 1

pnavk
pnavk

Reputation: 4632

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

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

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.

Upvotes: 2

Related Questions