ios 在嵌入 UITableViewCell 时告诉 UITextField 成为第一响应者
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1732798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Telling a UITextField to become first responder while embedded in a UITableViewCell
提问by Jasarien
I have a UITextField
that is a subview of a UITableViewCell
.
我有一个UITextField
是 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?
NO
无论何时调用,这都会返回,根据文档,这意味着文本字段拒绝成为第一响应者......我做错了什么?
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.
在textfield
正确地响应被触摸。然后它会调出键盘。只有在以becomefirstresponder
编程方式告诉它问题发生时。
采纳答案by Jasarien
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.
事实证明,当我尝试发送第一响应者时,文本字段为零。becomeFirstResponder
在生命周期的后期发送工作。
回答by Jim Rhoades
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:
假设您在 cellForRowAtIndexPath 方法中为文本字段指定了一个标签,您可以执行以下操作:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
myTextField = (UITextField *)[cell viewWithTag:kMyTextFieldTag];
[myTextField becomeFirstResponder];
}
回答by miracules
Calling it in the -(void)viewDidLoad would work.
在 -(void)viewDidLoad 中调用它会起作用。
-(void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.myTextField becomeFirstResponder]
//... any other code you need
}
回答by Justin Kramer
I was not getting the cursor to show up when the UITextField
was made first responder inside of the cellForRowAtIndexPath
. 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.
当UITextField
在cellForRowAtIndexPath
. 文本字段可以正常工作,但它没有在字段内显示闪烁的光标。当您按住时,缩放放大也不会显示光标。
I got around this odd issue by setting my becomeFirstResponder
inside of the didSelectRowAtIndex
method.
我通过设置方法becomeFirstResponder
内部来解决这个奇怪的问题didSelectRowAtIndex
。
回答by Art Gillespie
Is your UITableView editing
property set to YES
?
您的 UITableViewediting
属性是否设置为YES
?