【问题标题】:Set focus and show keyboard programmatically in xcode?在xcode中以编程方式设置焦点并显示键盘?
【发布时间】:2015-06-16 13:42:28
【问题描述】:

这是我在单击 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 上。

【问题讨论】:

  • 我已经检查了你的代码在这里工作正常,我认为 txtcom.section=index 行中存在一些问题,只需将其删除并检查它
  • txtcom.section=index 用于分组,因此无法删除。在我身边,为什么键盘无法显示。
  • 我已经使用了您的代码,只需删除该部分行,以便单击按钮后它会完美运行
  • 你把 becomefirstresponder 放在哪里了?在 addSubview: 之后或 addSubview: 之前?

标签: objective-c xcode uitextfield


【解决方案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];

}

【讨论】:

  • 文本字段仅在单击按钮后显示。是你的点击按钮吗?
  • yap,文本字段正在显示,但光标未集中在其框上,并且键盘未显示。
  • 确保你在类中符合 UITextFieldDelegate 协议
猜你喜欢
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 2011-08-26
  • 2021-10-30
  • 1970-01-01
相关资源
最近更新 更多