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


当前回答

比Meagar的回答更简单

覆盖touchesBegan: withEvent:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [textField resignFirstResponder];`
}

当你在背景中触摸任何地方时,这将使键盘消失。

其他回答

Try:

[self.view endEditing:YES];

你必须使用其中一种方法,

[self.view endEditing:YES];

or

[self.textField resignFirstResponder];

你可以向应用程序发送一个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中也有使用。

最简单的方法是调用方法

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

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

是的,终止是最好的选择。从iOW 7.0开始,UIScrollView有一个很酷的功能,可以在与滚动视图交互时取消键盘。为此,你可以设置UIScrollView的keyboardDismissMode属性。

设置键盘解散模式为:

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag

它几乎没有其他类型。看看这个苹果文档。