动态插入更多UITextField

时间:2013-03-21 04:45:45

标签: iphone ios cocoa-touch uiscrollview

我有附加屏幕截图中描述的要求。单击蓝色添加按钮时,必须在最后一个文本字段和textview之间插入一个UItextfield,并且必须在动态插入的新uitextfield旁边显示蓝色添加按钮。任何人都可以建议如何实现这一目标。我已将所有字段都放在UIScrollview

enter image description here

未启用滚动视图:

enter image description here

5 个答案:

答案 0 :(得分:4)

你可以这样做:

在.h我有一个名为previousTextField的插座,它与第一个连接。顾名思义它会存储最新添加的textField。

查找正在运行的project here

-(IBAction)addTextField:(id)sender{
    float x=previousTextField.frame.origin.x;
    float y=previousTextField.frame.origin.y+50;//get y from previous textField and add 10 or so in it.
    float w=previousTextField.frame.size.width;
    float h=previousTextField.frame.size.height;
    CGRect frame=CGRectMake(x,y,w,h);
    UITextField *textField=[[UITextField alloc] initWithFrame:frame];
    textField.placeholder = @"Enter User Name or Email";


    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.font = [UIFont systemFontOfSize:15];
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;


    [self.view addSubview:textField];
    previousTextField=textField;
}

只是一个想法/算法如何使用,而不是编译器检查

我错过了什么,改变了 + 按钮的位置。我想你可以做到:)

编辑:在上述方法中添加以下内容

//move addbutton which is an outlet to the button
CGRect frameButton=CGRectMake(addButton.frame.origin.x, addButton.frame.origin.y+50, addButton.frame.size.width, addButton.frame.size.height);
[addButton removeFromSuperview];
[addButton setFrame:frameButton];
[self.view addSubview:addButton];

答案 1 :(得分:0)

在“添加”按钮上,在子视图中添加textfeild,并为textfeild提供唯一标记,以便它可以帮助您区分所有textfeild。对于设置框架,请参考最后一个textfeild框架,并相应地设置y坐标。

UITextField *textField = [[UITextField alloc] initWithFrame:yourFrame];
textField.delegate = self;
textField.tag=yourtag;
[scrollview addSubview:textField];
[textField release];//if arc is disable

答案 2 :(得分:0)

首先获取UItextField的框架。现在增加前一个textFeild的yPos和UITetField的新位置。并重新排列低于该textField的UITextField,只需增加每个TextField的yPos。

希望这会对你有所帮助。

一切顺利!!!

答案 3 :(得分:0)

查看我的代码的输出.. enter image description here

您可以使用以下代码...我已经完成了它&它按照您的屏幕截图运行.. 在你的.h文件中只是声明这个变量

      int newY,newY1;
      UIButton *btn;

然后viewDidLoad方法中的.m文件添加以下代码

   UITextField *txt=[[UITextField alloc]init];
   txt.frame=CGRectMake(50,30,140,30);
   txt.placeholder = @"Enter User Name or Email";
   txt.borderStyle = UITextBorderStyleRoundedRect;

   [self.view addSubview:txt];
   newY=txt.frame.origin.y;
   btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
   btn.frame=CGRectMake(250,30, 30, 30);
   [btn addTarget:self action:@selector(addtxtField) forControlEvents:UIControlEventTouchUpInside];
   [self.view addSubview:btn];

&安培;然后创建一个addtxtField方法..见下面

 -(void)addtxtField
{
newY=newY+50;

UITextField *nwtxt=[[UITextField alloc]init];
nwtxt.frame=CGRectMake(50,newY,140,30);
nwtxt.placeholder = @"Enter User Name or Email";
nwtxt.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:nwtxt];

btn.frame=CGRectMake(250,newY, 30, 30);
[self.view addSubview:btn];

}

根据您的要求,它是100%运行...

答案 4 :(得分:0)

Output

试试这个::

在.h文件中

@property(nonatomic, retain) IBOutlet UIScrollView *scr;
@property(nonatomic, retain) IBOutlet UITextField *txt;
@property(nonatomic, retain) IBOutlet UITextView *txtVw;
@property(nonatomic, retain) IBOutlet UIButton *btn;

-(IBAction)click:(id)sender;

在.m文件中

int cnt;

float x = 0.0, y = 0.0, w = 0.0, h = 0.0;

- (void)viewDidLoad
{
    [super viewDidLoad];

    scr.delegate = self;
    scr.contentSize = CGSizeMake(0, 400);

    cnt = 0;
}

-(IBAction)click:(id)sender
{
    if (cnt == 0) {
       x=txt.frame.origin.x;
       y=txt.frame.origin.y + 50;//get y from previous textField and add 10 or so in it.
       w=txt.frame.size.width;
       h=txt.frame.size.height;
    }
    else
    {
       y+=50;
    }

    UITextField *textField=[[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"Enter User Name or Email";

    [scr addSubview:textField];

    [btn setFrame:CGRectMake(248, y, 30, 30)];

    float y1 = y + 100;
    [txtVw setFrame:CGRectMake(orign_x, y1, origin_w, origin_h)];

    cnt++;
}

希望,它肯定会帮助你。

感谢。