如何选择UITextField? [英] How to select a UITextField?

查看:94
本文介绍了如何选择UITextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UITableView中编辑用户的用户名。我在UITableViewCell中添加了一个UITextField,这似乎非常好用。但是当用户触摸单元格时(甚至在文本区域外),他希望编辑。

I want to edit the user's username inside a UITableView. I added a UITextField to the UITableViewCell and this seems to work very nicely. But when the user touches the cell (even outside the textfield), he expects to edit.

如何以编程方式选择文本字段?

How do I programatically select the textfield?

代码看起来像:

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
userName.selected = TRUE;
}


推荐答案

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
   UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath: indexPath];
   UITextField *textField = [[cellSelected.contentView subviews] objectAtIndex: 0];
   [textField becomeFirstResponder];

   [tableView deselectRowAtIndexPath: indexPath animated: NO];

}

这假设你没有更快的方法知道哪个UITextField对象在哪个单元格中,并且您确定UITextField将是第一个子视图。

This assumes that you have no quicker way to know which UITextField object is in which cell, and that you know for sure that the UITextField will be the first subview.

您还可能要检查[textField isFirstResponder] - 如果它已经是第一个响应者,那就没有意义了。但这可能不是必要的。

You also might want to put in a check for [textField isFirstResponder] -- no point in making it the firstResponder if it already is. This may not be necessary though.

这篇关于如何选择UITextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆