[Solved] How to add multiple UITextfield in iOS 5?


Put it in a loop, offset each text field’s Y position and tag each text field:

for (int i = ; i < numberOfTextFieldsNeeded; i++) {
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(5, 5 + 35 * i ,100,25)]; // 10 px padding between each view
    tf.tag = i + 1; // tag it for future reference (+1 because tag is 0 by default which might create problems)
    tf.borderStyle = UITextBorderStyleRoundedRect;
    [tf setReturnKeyType:UIReturnKeyDefault];
    [tf setEnablesReturnKeyAutomatically:YES];
    [tf setDelegate:self];
    [self.view addSubview:tf]
    // don't forget to do [tf release]; if not using ARC
}

Then in delegate methods perform actions based on tag of the textField that called each delegate method. For example to switch to next text view when user taps return key:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    UITextField *nextTextField = [self.view viewWithTag:textField.tag + 1];
    [nextTextField becomeFirstResponder];
}

Keep in mind that sending messages to nil in Objective-C will not crash so it will be perfectly fine when user taps return key in last text field as UITextField *nextTextField = [self.view viewWithTag:textField.tag + 1]; will return nil, and calling becomeFirstResponder on nil will do nothing. But you can check if nextTextField is nil and do something else then, whatever you like.

1

solved How to add multiple UITextfield in iOS 5?