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


当前回答

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

这是一个链接

其他回答

我讨厌没有“全局”的方法在不使用私有API调用的情况下以编程方式取消键盘。我经常需要在不知道哪个对象是第一个响应器的情况下以编程方式关闭键盘。我使用Objective-C运行时API检查self,枚举它的所有属性,取出那些类型为UITextField的属性,并将resignFirstResponder消息发送给它们。

做这件事不应该这么难…

老实说,我对这里提出的任何解决方案都不感兴趣。我确实发现了一种使用TapGestureRecognizer的好方法,我认为它解决了问题的核心:当你点击键盘以外的任何东西时,忽略键盘。

In viewDidLoad, register to receive keyboard notifications and create a UITapGestureRecognizer: NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil]; [nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil]; tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)]; Add the keyboard show/hide responders. There you add and remove the TapGestureRecognizer to the UIView that should dismiss the keyboard when tapped. Note: You do not have to add it to all of the sub-views or controls. -(void) keyboardWillShow:(NSNotification *) note { [self.view addGestureRecognizer:tapRecognizer]; } -(void) keyboardWillHide:(NSNotification *) note { [self.view removeGestureRecognizer:tapRecognizer]; } The TapGestureRecognizer will call your function when it gets a tap and you can dismiss the keyboard like this: -(void)didTapAnywhere: (UITapGestureRecognizer*) recognizer { [textField resignFirstResponder]; }

这个解决方案的优点是它只过滤轻敲,而不是滑动。因此,如果你在键盘上方有滚动内容,滑动仍然会滚动并显示键盘。通过在键盘消失后移除手势识别器,未来在视图上的点击将被正常处理。

杰里米的回答不太适合我,我想是因为我在一个选项卡视图中有一个导航堆栈,上面有一个模态对话框。我现在使用下面的,它是为我工作,但你的里程可能会有所不同。

 // dismiss keyboard (mostly macro)
[[UIApplication sharedApplication].delegate dismissKeyboard]; // call this in your to app dismiss the keybaord

// --- dismiss keyboard (in indexAppDelegate.h) (mostly macro)
- (void)dismissKeyboard;

// --- dismiss keyboard (in indexAppDelegate.m) (mostly macro)
// do this from anywhere to dismiss the keybard
- (void)dismissKeyboard {    // from: http://stackoverflow.com/questions/741185/easy-way-to-dismiss-keyboard

    UITextField *tempTextField = [[UITextField alloc] initWithFrame:CGRectZero];

    UIViewController *myRootViewController = <#viewController#>; // for simple apps (INPUT: viewController is whatever your root controller is called.  Probably is a way to determine this progragrammatically)
    UIViewController *uivc;
    if (myRootViewController.navigationController != nil) { // for when there is a nav stack
        uivc = myRootViewController.navigationController;
    } else {
        uivc = myRootViewController;
    }

    if (uivc.modalViewController != nil) { // for when there is something modal
        uivc = uivc.modalViewController;
    } 

    [uivc.view  addSubview:tempTextField];

    [tempTextField becomeFirstResponder];
    [tempTextField resignFirstResponder];
    [tempTextField removeFromSuperview];
    [tempTextField release];

}

这里有很多过于复杂的答案,也许是因为这在iOS文档中不容易找到。约瑟夫在上面写着:

[[view window] endEditing:YES];

Try:

[self.view endEditing:YES];