在xcode中以编程方式设置焦点并显示键盘?

时间:2015-06-16 04:30:55

标签: objective-c xcode uitextfield

这是我的编码,单击UIButton时以编程方式创建UITextField。但是为了增强,我想在创建UITextField时将焦点(和显示键盘)设置为新的UITextField。

UITextField * txtComp=[[UITextField alloc]initWithFrame:CGRectMake(20,y, 450, 50)];
txtComp.borderStyle=UITextBorderStyleNone;
txtComp.delegate = self;
txtComp.tag=i+1;
txtComp.section=index;
txtComp.placeholder = @"New UITextField";
[txtComp becomeFirstResponder];

上面的编码工作是创建新的UITextField,但是即使设置了 becomeFirstResponder ,也无法将注意力集中在新的UITextField上。

1 个答案:

答案 0 :(得分:0)

-(IBAction)buttonClicked:(id)sender

{

UITextField * txtComp=[[UITextField alloc]initWithFrame:CGRectMake(20,20, 450, 50)];
txtComp.borderStyle=UITextBorderStyleRoundedRect;
txtComp.delegate = self;


txtComp.placeholder = @"New UITextField";
[txtComp becomeFirstResponder];
[self.view addSubview:txtComp];
[self.view bringSubviewToFront:txtComp];

}