在嵌入UITableViewCell时,告诉UITextField成为第一响应者

时间:2022-02-18 16:52:36

I have a UITextField that is a subview of a UITableViewCell.

我有一个UITextField,它是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.

文本字段正确响应被触摸。它然后调出键盘。只有在告诉它以编程方式成为第一响应者时才会发生问题。

5 个解决方案

#1


41  

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.

事实证明,当我试图将它发送给第一响应者时,文本字段为零。在生命周期的后期发送成为FirstFirstResponder。

#2


8  

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方法中为textfield指定了标记,则可以执行以下操作:

- (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];
}

#3


4  

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 
}

#4


1  

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.

通过在didSelectRowAtIndex方法中设置我的becomeFirstResponder,我解决了这个奇怪的问题。

#5


0  

Is your UITableView editing property set to YES?

您的UITableView编辑属性是否设置为YES?

#1


41  

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.

事实证明,当我试图将它发送给第一响应者时,文本字段为零。在生命周期的后期发送成为FirstFirstResponder。

#2


8  

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方法中为textfield指定了标记,则可以执行以下操作:

- (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];
}

#3


4  

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 
}

#4


1  

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.

通过在didSelectRowAtIndex方法中设置我的becomeFirstResponder,我解决了这个奇怪的问题。

#5


0  

Is your UITableView editing property set to YES?

您的UITableView编辑属性是否设置为YES?