Search code examples
iphoneiosipadios5ios6

adding uitextfields dynamically at runtime


I want to generate textfields dynamically in my app scenario is I am having a textfield and a button if entered the number 2 in textfield and pressed the button, then 2 texfields must be generated on view controller


Solution

  • You can achieve this using this code;

    int num=yournum;
    for (int i = 1; i <= num; i++) {
    
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(...)];
        textField.tag = i;
        ...
        [self.view addSubview:textField];
    }
    

    And to access the correct text field afterwards you'd call [self.view viewWithTag:tag].