动态插入更多 UITextFields

Dynamically insert more UITextFields(动态插入更多 UITextFields)

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

问题描述

我有一个附加屏幕截图中描述的要求.单击蓝色添加按钮时,必须在最后一个文本字段和 textview 之间插入一个 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 的插座,它连接到第一个插座.顾名思义,它将存储最新添加的文本字段.

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.

在这里找到正在运行的项目.

Find running 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 :)

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

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

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

本文标题为:动态插入更多 UITextFields

基础教程推荐