C中的按钮时动态添加文本字段

C中的按钮时动态添加文本字段

本文介绍了单击Objective-C中的按钮时动态添加文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮选择器视图功能的文本字段.

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.

推荐答案

您正在创建新的文本字段,但将它们添加到彼此之上.

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;

并在此行中使用此索引

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.

这篇关于单击Objective-C中的按钮时动态添加文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 06:41