我有相当多的控件分散在我的表格中的许多表格单元格中,我想知道是否有一种更简单的方法来消除键盘,而不必循环遍历所有的控件并将它们全部辞职为第一响应器。我想问题是…我如何得到当前的第一个响应器的键盘?


当前回答

最简单的方法是调用方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    if(![txtfld resignFirstResponder])
    {
        [txtfld resignFirstResponder];
    }
    else
    {

    }
    [super touchesBegan:touches withEvent:event];
}

其他回答

添加一个点击手势识别器到您的视图。并定义为ibaction

你的。m文件就像

    - (IBAction)hideKeyboardGesture:(id)sender {
    NSArray *windows = [UIApplication sharedApplication].windows;
    for(UIWindow *window in windows) [window endEditing:true];
    [[UIApplication sharedApplication].keyWindow endEditing:true];
}

这对我很有效

Try:

[self.view endEditing:YES];

你可能也需要覆盖UIViewController disablesautomatickeyboarddismiss来让它在某些情况下工作。如果你有UINavigationController,这可能必须在UINavigationController上完成。

你可以向应用程序发送一个nil目标动作,它会在任何时候辞职第一响应者,而不必担心哪个视图当前有第一响应者状态。

objective - c:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

斯威夫特3.0:

UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil)

在Mac OS X中,Nil目标操作在菜单命令中很常见,在iOS中也有使用。

关于如何在iOS中当用户触摸屏幕上UITextField或键盘之外的任何地方时取消键盘的快速提示。考虑到iOS键盘所占用的空间,为用户提供一种简单直观的方式来消除键盘是有意义的。

这是一个链接