首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >becomeFirstResponder不在iOS 8工作

becomeFirstResponder不在iOS 8工作
EN

Stack Overflow用户
提问于 2014-11-24 04:55:07
回答 8查看 22.4K关注 0票数 20

我正在使用UITextField的方法becomeFirstResponder来显示键盘。这在iOS 7中是可行的,但在iOS 8中,这种方法没有显示键盘。

代码语言:javascript
复制
 UITextField *txtAddNew = [[UITextField alloc] initWithFrame:CGRectMake(10,10,240, 21)];
 txtAddNew.font = [UIFont fontWithName:@"Helvetica" size:16];
 txtAddNew.clearButtonMode = UITextFieldViewModeWhileEditing;
 txtAddNew.returnKeyType = UIReturnKeyDone;
 txtAddNew.delegate = self;
 txtAddNew.autocorrectionType = UITextAutocorrectionTypeNo;
 txtAddNew.tag = 15;

 // Open keyboard
 [txtAddNew becomeFirstResponder];

在iOS 8中有什么方法可以做到吗?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2014-11-24 05:12:40

尝试按下面的方式调用becomeFirstResponder

代码语言:javascript
复制
[txtAddNew performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0];

按照苹果的说法,

只有当当前响应者可以辞职第一响应者状态(canResignFirstResponder)并且新的响应者可以成为第一响应者时,响应对象才会成为第一响应者。 您可以调用此方法使响应器对象(如视图)成为第一个响应程序。但是,只有当该视图是视图层次结构的一部分时,才应该对其进行调用。如果视图的window属性包含一个UIWindow对象,则它已安装在视图层次结构中;如果它返回零,则该视图将与任何层次结构分离。

票数 48
EN

Stack Overflow用户

发布于 2016-12-19 09:29:23

Swift 3

这是被接受的答案的快速3版本。为了让它开始工作,我还得加点时间。

代码语言:javascript
复制
txtAddNew.perform(
    #selector(becomeFirstResponder), 
    with: nil, 
    afterDelay: 0.1
)
票数 17
EN

Stack Overflow用户

发布于 2014-11-24 05:16:28

我想你错过了addsubview the txtAddNew to the Mainview

代码语言:javascript
复制
   txtAddNew.tag = 15;
   [self.view addsubview:txtAddNew];  

   // Open keyboard
  [txtAddNew becomeFirstResponder];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27098097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档