问题
Here is my coding that UITextField is programmatically created when click on UIButton. But to enhancement is I want to set focus (and show keyboard) to new UITextField when UITextField is being created.
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];
Above coding work as creating new UITextField but cannot be set focus on new UITextField at all even though becomeFirstResponder is set.
回答1:
-(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];
}
来源:https://stackoverflow.com/questions/30858832/set-focus-and-show-keyboard-programmatically-in-xcode