告诉 UITextField 在嵌入 UITableViewCell 时成为第一响应者
我有一个 UITextField
,它是 UITableViewCell
的子视图。
当我的视图加载时,我希望文本字段成为第一响应者。我有一个指向表格单元格中文本字段的指针,因此为了执行此操作,我正在尝试:
[myTextField becomeFirstResponder];
无论何时调用它,它始终返回 NO
,根据文档,这意味着文本字段拒绝成为第一响应者...我做错了什么?
编辑:
textfield
正确响应被触摸。然后它会调出键盘。只有当以编程方式告诉它becomefirstresponder
时才会出现问题。
I have a UITextField
that is a subview of a UITableViewCell
.
When my view loads, I want the text field to become first responder. I have a pointer to the text field in the table cell, so to do this I am trying:
[myTextField becomeFirstResponder];
This returns NO
all the time, regardless of when it's called, which according to the docs means the text field refused to become first responder... What am I doing wrong?
Edit:
The textfield
properly responds to being touched. It bring up the keyboard then. It's only when telling it to becomefirstresponder
programmatically that the problem happens.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
事实证明,当我尝试将其发送给第一响应者时,文本字段为零。
在生命周期的后期发送
becomeFirstResponder
是有效的。It turned out that the text field was nil when I was trying to send it first responder.
Sending
becomeFirstResponder
later in the life cycle worked.我对作为子视图添加到分组表视图单元格的文本字段遇到了同样的问题 - 但经过一番摸索后找到了解决方案。
假设您已在 cellForRowAtIndexPath 方法中为文本字段指定了标签,则可以执行以下操作:
I was having the same problem with a textfield I had added as a subview to a grouped table view cell - but figured out a solution after some poking around.
Assuming you have given the textfield a tag in your cellForRowAtIndexPath method, you can do something like this:
在 -(void)viewDidLoad 中调用它就可以了。
Calling it in the -(void)viewDidLoad would work.
当
UITextField
在cellForRowAtIndexPath
内成为第一响应者时,我没有显示光标。文本字段可以正常工作,但它没有显示字段内闪烁的光标。当您按住时,缩放放大也不会显示光标。我通过在
didSelectRowAtIndex
方法中设置becomeFirstResponder
解决了这个奇怪的问题。I was not getting the cursor to show up when the
UITextField
was made first responder inside of thecellForRowAtIndexPath
. The text field would work just fine and dandy, but it was not showing the blinking cursor inside of the field. When you would press and hold, the zoom magnify would also not show a cursor.I got around this odd issue by setting my
becomeFirstResponder
inside of thedidSelectRowAtIndex
method.您的 UITableView
editing
属性是否设置为YES
?Is your UITableView
editing
property set toYES
?