inforeqd
inforeqd

Reputation: 3249

UItextView dismiss keyboard on tap outside the textview

i have a custom UIView which is presented from a UIViewController. This view controller can present one of several such views. One of these views has a UITextView. when one begins entering text in the UITextView, the keyboard is shown. however, I want to dismiss the keyboard whenever the user taps anywhere outside the textview. as multiple UIViews can be presented by the UIViewController based on certain condition, I encapsulated the UITextView delegate methods etc in the custom UIView, I did this using Notifications from within the custom UIView like

- (void)configureView
{ 
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

[nc addObserver:self selector:@selector(keyboardWillShow:) name:
 UIKeyboardWillShowNotification object:nil];

[nc addObserver:self selector:@selector(keyboardWillHide:) name:
 UIKeyboardWillHideNotification object:nil];

self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                           action:@selector(didTapAnywhere:)];
}

-(void) keyboardWillShow:(NSNotification *) note {
[self addGestureRecognizer:self.tapRecognizer];
}

-(void) keyboardWillHide:(NSNotification *) note
{
[self removeGestureRecognizer:self.tapRecognizer];
}

-(void)didTapAnywhere: (UITapGestureRecognizer*) recognizer {    
[self.detailsTextView resignFirstResponder];
}

however, when the didTapAnywhere is called, the keyboard does not get dismissed even when the resignfirstresponder ties place. FYI, the delegate for the UITextView is the custom UIView.

ple help in how to do this

Upvotes: 3

Views: 5580

Answers (2)

Dilip Rajkumar
Dilip Rajkumar

Reputation: 7074

You have to include UITextViewDelegate to your class and detailsTextView.delegate = self in your viewDidLoad or any ware you initialise your detailsTextView.. I hope this will solve your problem...

Upvotes: 1

BBog
BBog

Reputation: 3650

Try

[self.view endEditing:YES];

instead of resigning the first responder. This should do the trick. What's weird is why isn't the standard approach working...

Upvotes: 5

Related Questions