2013-03-21 76 views
0

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

enter image description here

滚动视图未启用:

enter image description here

+0

你有没有试过任何想法? – Kalyani 2013-03-21 05:12:43

+0

检查我的答案,将按照您的屏幕截图运行sucessully .. – Kalyani 2013-03-21 05:57:30

回答

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]; 
+0

感谢您的回答,我会尝试并找回你 – 2013-03-21 04:53:58

+0

我收到以下错误...使用未声明的标识符NSFrame。我必须导入任何包或NSFrame宣布的位置 – 2013-03-21 05:09:52

+0

对不起,它应该是NSMakeRect,我只是在这里输入而不检查:p – 2013-03-21 05:11:07

0

添加按钮下面的子视图中添加textfeild并给予textfeild一个独特的标记,以便它会帮助你区分所有文本。为了设置框架,请参考上一个文本框架框架并相应地设置y坐标。

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

首先获取UItextField的框架。现在增加以前的textFeild的yPos和UITetField的新位置。重新安排位于textField下方的UITextField只需增加每个TextField的yPos。

希望这会帮助你。

所有最好的!

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%运行,按您的requiement ...

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++; 
} 

希望它能一定帮助你。

谢谢。

+0

然后根据文本字段设置滚动视图的比例。 – 2013-03-21 06:12:00

+0

以及新的文本框,我们必须创建新的按钮。当用户点击新的按钮时,应该出现另一个文本框 – 2013-03-21 06:31:07

+0

@RamuPasupuleti:按屏幕,按钮正在移动。我的代码正在以这种方式工作。或者你可以添加新的按钮。但我认为,那不是你的需要。 – 2013-03-21 07:06:27