【问题标题】:Proper way to resignFirstResponder when a UITableView is touched?触摸 UITableView 时 resignFirstResponder 的正确方法?
【发布时间】:2010-05-29 18:18:49
【问题描述】:

我有一个非常简单的UITableView,其单元格包含UITextFields,每当用户触摸其中一个单元格之外的 UITableView 时,我需要能够调用resignFirstResponder 来隐藏键盘。

我已经阅读了this question/answer,但这似乎是实现这一目标的一种非常基本的方法。我已经阅读了一种将UITableView 转换为UIControl 的方法,以便您可以连接TouchDown 事件。

有人知道实现此功能的标准或首选方法吗?

【问题讨论】:

    标签: iphone uitableview uitextfield


    【解决方案1】:

    点击手势识别器会检测到点击,但是我有时发现它会干扰正常的行选择机制。我想我在answer to this question. 中提出了一个很好的解决方案,我在其中发布了一些UIView 子类的示例代码,您将其设置为文本字段的accessoryInputView。然后,如果您选择文本字段,它会自动添加点击识别器,然后在您完成后自动将其删除。

    【讨论】:

    • 感谢您的回答。我不再做 iPhone 开发了,但很高兴知道这个问题得到了正确的回答 :-)
    【解决方案2】:

    这很有趣,但不是很难,因为你必须使用 UITapGestureRecognizer...

    在.h中

    IBOutlet UITextField *txtField;
    IBOutlet UITableView *tableview;
    

    在.m

    -(void)viewDidLoad
    {
    
    UITapGestureRecognizer *tapgesture=[[UITapGestureRecognizer alloc]initWithTarget:self 
    action:@selector(tableClicked)];
    [tableview addGestureRecognizer:tapgesture];
    
    [super viewDidLoad];
    
    }
    
    -(void)tableClicked
    {
    
         [txtField resignFirstResponder];
    
    }
    

    请不要使用这个示例代码,我认为它可以工作...谢谢..

    【讨论】:

    • 这似乎可行,但最终它会阻止选择表格视图单元格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 2011-09-27
    • 2011-08-16
    相关资源
    最近更新 更多