Adding text field dynamically when clicked on button in Objective-C

痞子三分冷 提交于 2019-12-12 03:55:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!