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

发布于 2024-08-11 03:33:52 字数 391 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

此刻的回忆 2024-08-18 03:33:52

事实证明,当我尝试将其发送给第一响应者时,文本字段为零。
在生命周期的后期发送 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.

╰ゝ天使的微笑 2024-08-18 03:33:52

我对作为子视图添加到分组表视图单元格的文本字段遇到了同样的问题 - 但经过一番摸索后找到了解决方案。

假设您已在 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];
}

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)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];
}
小伙你站住 2024-08-18 03:33:52

在 -(void)viewDidLoad 中调用它就可以了。

-(void)viewDidLoad{
   [super viewDidLoad];
   // Do any additional setup after loading the view.
   [self.myTextField becomeFirstResponder] 

   //... any other code you need 
}

Calling it in the -(void)viewDidLoad would work.

-(void)viewDidLoad{
   [super viewDidLoad];
   // Do any additional setup after loading the view.
   [self.myTextField becomeFirstResponder] 

   //... any other code you need 
}
っ左 2024-08-18 03:33:52

UITextFieldcellForRowAtIndexPath 内成为第一响应者时,我没有显示光标。文本字段可以正常工作,但它没有显示字段内闪烁的光标。当您按住时,缩放放大也不会显示光标。

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

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.

I got around this odd issue by setting my becomeFirstResponder inside of the didSelectRowAtIndex method.

绿光 2024-08-18 03:33:52

您的 UITableView editing 属性是否设置为 YES

Is your UITableView editing property set to YES?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文