问题
I have a text field with button pickerView functionality.
if(pickerView==selectItemListPickerView)
{
selectITemListTxtFld.text = [itemListSplitDisplayArray objectAtIndex:row];
}
Now I want to display this functionality in another dynamically created text field each time when I click on add button. I tried it like this.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(18, 350, 309, 35)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
textField.tag = 44;
[self.view addSubview:textField];
Like this I am getting only one text field dynamically. So please do the needful.
回答1:
You are creating new text fields but adding them on top of each other.
Consider keeping an index as global variable in the class/controller
{
NSInteger textFieldCount = 0;
}
After you create a text field increment it
textFieldCount += 1;
And use this index in this line
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(18, 350 + textFieldCount*35, 309, 35)];
You will add text fields one step down each time you create new one.
来源:https://stackoverflow.com/questions/35839327/adding-text-field-dynamically-when-clicked-on-button-in-objective-c