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


当前回答

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

 // 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];

}

其他回答

这是一个解决方案,使键盘离开时,点击返回在任何文本字段,通过添加代码在一个地方(所以不必为每个文本字段添加一个处理程序):


考虑一下这个场景:

我有一个带有两个文本字段(用户名和密码)的视图控制器。 并且视图控制器实现UITextFieldDelegate协议

我在viewDidLoad中这样做

- (void)viewDidLoad 
{
    [super viewDidLoad];

    username.delegate = self;
    password.delegate = self;
}

而视图控制器实现了可选方法as

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

不管你在哪个文本域,只要我按下回车键,它就会消失!

在你的情况下,同样的工作,只要你设置所有的文本字段的委托自我和实现textFieldShouldReturn

更新

我找到了另一种简单的方法

简单地声明一个属性:-

@property( strong , nonatomic) UITextfield *currentTextfield;

和一个Tap Gesture Gecognizer:-

@property (strong , nonatomic) UITapGestureRecognizer *resignTextField;

在ViewDidLoad

_currentTextfield=[[UITextField alloc]init];
_resignTextField=[[UITapGestureRecognizer alloc]initWithTarget:@selector(tapMethod:)];

[self.view addGestureRecognizer:_resignTextField];

实现文本字段委托方法didBeginEditing

 -(void)textFieldDidBeginEditing:(UITextField *)textField{


      _currentTextfield=textField;

    }

实现点击手势方法(_resignTextField)

 -(void)tapMethod:(UITapGestureRecognizer *)Gesture{

     [_currentTextfield resignFirstResponder];

 }

比Meagar的回答更简单

覆盖touchesBegan: withEvent:

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

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

@Nicholas Riley &@Kendall Helmstetter Geln & @cannyboy:

绝对的辉煌!

谢谢你!

考虑到你和其他人在这篇文章中的建议,以下是我所做的:

使用时的样子:

[[self appDelegate]解聘键盘];(注意:我添加了appDelegate作为NSObject的补充,所以我可以在任何地方使用任何东西)

引擎盖下的样子:

- (void)dismissKeyboard 
{
    UITextField *tempTextField = [[[UITextField alloc] initWithFrame:CGRectZero] autorelease];
    tempTextField.enabled = NO;
    [myRootViewController.view addSubview:tempTextField];
    [tempTextField becomeFirstResponder];
    [tempTextField resignFirstResponder];
    [tempTextField removeFromSuperview];
}

EDIT

修正我的回答包括tempTextField。enabled = NO;。禁用文本字段将阻止UIKeyboardWillShowNotification和UIKeyboardWillHideNotification键盘通知发送,如果你在整个应用程序中依赖这些通知。

您可以递归地遍历子视图,存储所有UITextFields的数组,然后循环遍历它们并重新分配它们。

这并不是一个很好的解决方案,特别是当你有很多子视图时,但对于简单的应用程序来说,它应该是可行的。

我用一种更复杂,但更高效的方式解决了这个问题,但使用了我的应用程序的动画引擎的单例/管理器,任何时候一个文本字段成为响应器,我会将它分配给一个静态,它会根据某些其他事件被清除(辞职)…我几乎不可能用一段话解释清楚。

要有创意,在我发现这个问题后,我只花了10分钟就考虑了这个问题。