Reputation: 6801
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?
Upvotes: 7
Views: 2686
Reputation: 1143
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];
}
Upvotes: 1
Reputation: 10096
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.
Upvotes: 0
Reputation: 4705
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.
Upvotes: 0