如何激活 UITableView 中的下一个 UITextField?
我正在使用这个其他答案来添加UITextField
到我的 UITableViewCell 。但是,我现在不知道如何让用户通过使用下一个按钮专注于下一个项目。有什么提示吗?
I am using this other SO answer for adding UITextField
s to my UITableViewCell
s. However, I am now at a loss how I can let the user focus on the next item by using the next button. Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试为每个 UITextField 分配一个
标签
。按下“下一步”按钮后,计算下一个标签并使用[[UIView viewWithTag:nextTag] comeFirstResponder]
将焦点更改到下一个文本字段。Try assigning a
tag
to each UITextField. Once the Next button is pressed, calculate the next tag and use[[UIView viewWithTag:nextTag] becomeFirstResponder]
to change the focus to the next text field.这对我来说效果很好,但必须进行调整以满足您的需求:
This worked well for me, would have to be tweaked to fit your needs though:
除了 Anh 发布的关于使用标签值查找下一个字段并将其设为第一个响应者之外...
如果您在 UITableView 中,您还需要记住下一个 UITextField 可能不在屏幕上,甚至不在屏幕上视图,因为它可能位于屏幕底部下方。您可能需要先将该新行滚动到视图中,然后再使其成为第一响应者。我在之前的应用程序中为处理此问题所做的操作是使标记成为一个值,我可以使用该值来暗示该行的 NSIndexPath,以便我可以找出它所在的行。然后您可以将该行滚动到视图中:
但是,这会创建一个竞争条件,当您调用BecomeFirstResponder时,单元格还不可见,因此我会延迟becomeFirstResponder,直到它可见。我会在我的类中设置一个值作为属性,例如应该成为第一响应者的字段的行号,然后在 cellForRowAtIndexPath 或 willDisplayCell:forRowAtIndexPath 中设置一个值,当我要显示该单元格时,我会调用变成FirstResponder文本字段 THEN... 因为这样它就保证存在。
In addition to what Anh posted about using the tag value to find the next field and make it firstResponder...
If you are in a UITableView, you also need to keep in mind that the next UITextField might not be on the screen or even in the view because it might be below the bottom of the screen. You might need to scroll that new row into view before making it first responder. What I did in a previous app to handle this was make the tag be a value that I could use to imply the NSIndexPath of the row so I could figure out which row it was in. Then you can scroll that row into view with:
However, this creates a race condition where the cell isn't visible yet when you call becomeFirstResponder, so I would delay the becomeFirstResponder until it is visible. I would set a value as a property in my class, like the row number, of the field which was supposed to become first responder and then in either cellForRowAtIndexPath or willDisplayCell:forRowAtIndexPath when I'm about to display that cell I would call becomeFirstResponder on the textfield THEN... because then it is guaranteed to exist.
你愿意吗..
Would do you like ..