动态插入更多UITextField [英] Dynamically insert more UITextFields

查看:137
本文介绍了动态插入更多UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,如附上的屏幕截图所述。当点击蓝色添加按钮时,必须在最后一个文本字段和文本视图之间插入另一个 UItextfield ,并且蓝色添加按钮将必须出现在动态插入的新uitextfield旁边。任何一个建议如何实现这一点。我已将所有字段放置在 UIScrollview

I have a requirement as described in the attached screen shot. When blue add button is clicked, one more UItextfield have to be inserted between last text field and textview and Blue add button will have to appear beside that dynamically inserted new uitextfield. Could any one suggest how to achieve this. I have placed all the fields in UIScrollview.

未启用滚动视图:

推荐答案

您可以这样做:

在.h中有一个出口 previousTextField 这是钩到第一个。顾名思义,它会存储最近添加的textField。

In .h I have an outlet called previousTextField which is hooked to the first one. As name suggests it will store the latest added 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;
}

只是一个想法/算法,不是编译器检查

我错过了,更改了 + 按钮的位置。我认为你可以这样做:)

I missed on thing, changing the location of + button. I think you can do it :)

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

//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];

这篇关于动态插入更多UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆