7

I have a view of a UIViewController which has a UITextField as a subview. The text field implements an event on UIControlEventEditingDidBegin. This event sets the inputAccessoryView of the text field and adds a "shadow" view on the view of the view controller to block interaction with the view.

The inputAccesoryView is a UIView with a UITextView as subview. The UITextView is set as firstResponder when the keyboard shows (registered on UIKeyboardDidShowNotification).

When the "shadow" view is touched I call the following method:

-(void)dismissKeyboard
{
    self.dimScreenView.alpha = 0.0f;
    self.writingView.txtView.text = @"";

    [self.writingView.txtView resignFirstResponder];
}

But the keyboard does not disappear when the shadow view is touched. I have tried calling the [self.writingView.txtWritingField endEditing:YES] and [self.writingView endEditing:YES], but I cannot make it work.

Should I do something special to make the keyboard disappear when the inputAccessoryView has a subview, that is firstResponder?

Update:

It turns out, that the UITextView and UITextField both returns NO on the isFirstResponder property, even if I do not call resignFirstResponder. How can none of the text views be firstResponder while the keyboard is still present?

ThomasCle
  • 6,792
  • 7
  • 41
  • 81

3 Answers3

1

like @malex said, after [self.writingView.txtView resignFirstResponder];, your UITextField becomes the first responder again. So you should add the code let the UITextField resign first responder too:

-(void)dismissKeyboard
{
    self.dimScreenView.alpha = 0.0f;
    self.writingView.txtView.text = @"";

    [self.writingView.txtView resignFirstResponder];
    [yourTextField resignFirstResponder];
}
Gon
  • 1,135
  • 12
  • 22
0

You mention UITextView is set as firstResponder but in the code you are passing resignFirstResponder to the textField instance. Try calling resignFirstResponder on the textview instance.

Adithya
  • 4,545
  • 3
  • 25
  • 28
  • Sorry, it might seem unclear in the code. The `txtWritingField` is the `UITextView`. I will edit the name to `txtView` to make it clear. – ThomasCle Feb 03 '14 at 08:38
0

After [self.writingView.txtView resignFirstResponder];

your textField becomes the first responder again and keyboard is on screen with former inputAccessoryView staying from textView. So you only need to reload it:

-(void)dismissKeyboard
{
    self.dimScreenView.alpha = 0.0f;
    self.writingView.txtView.text = @"";

    [self.writingView.txtView resignFirstResponder];

    self.textField.inputAccessoryView = nil;
    [self.textField reloadInputViews];
}

In this way you can hide inputAccessoryView but you need another way to make it the first responder again because focus moves to textField immediately.

malex
  • 9,874
  • 3
  • 56
  • 77
  • 1
    It sounds reasonable, but I can't make the accessoryView disappear, which made me investigate the `firstResponder`. It turns out, that the `UITextView` and `UITextField` both returns `NO` on the `isFirstResponder` property, even if I do not call `resignFirstResponder`. That seems very strange to me. Do you have any idea why? – ThomasCle Feb 03 '14 at 09:53
  • 1
    In my model case UITextField has viewable focus after [UITextView resignFirstResponder]. It is strange if both of your fields don't have. So in my case self.textField isFirstResponder and I reload input view. – malex Feb 03 '14 at 12:07