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


当前回答

最简单的方法是调用方法

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

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

其他回答

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

[[view window] endEditing:YES];

更新

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

简单地声明一个属性:-

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

 }

老实说,我对这里提出的任何解决方案都不感兴趣。我确实发现了一种使用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]; }

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

子类化你的文本字段…还有textviews

在子类中放入此代码..

-(void)conformsToKeyboardDismissNotification{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissKeyBoard) name:KEYBOARD_DISMISS object:nil];
}

-(void)deConformsToKeyboardDismissNotification{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:KEYBOARD_DISMISS object:nil];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self resignFirstResponder];
}

在textfield委托中(类似于textview委托)

-(void)textFieldDidBeginEditing:(JCPTextField *)textField{
     [textField conformsToKeyboardDismissNotification];
}


- (void)textFieldDidEndEditing:(JCPTextField *)textField{
    [textField deConformsToKeyboardDismissNotification];
}

都准备好了. .现在只需从代码中的任何地方发布通知。它将放弃任何键盘。

更好的方法是让某些东西“窃取”第一响应者的地位。

因为UIApplication是UIResponder的子类,你可以尝试:

[[UIApplication sharedApplication] becomeFirstResponder]
[[UIApplication sharedApplication] resignFirstResponder]

如果做不到这一点,创建一个新的UITextField与一个零大小的框架,将它添加到一个视图的某处,并做一些类似的事情(成为后跟辞职)。