3

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

4

2 回答 2

5

尝试

[self.view endEditing:YES];

而不是辞去第一响应者的职务。这应该可以解决问题。奇怪的是为什么标准方法不起作用......

于 2012-05-16T09:10:06.313 回答
1

你必须包括UITextViewDelegate在你的班级和detailsTextView.delegate = self你的viewDidLoad或任何你初始化你的 detailsTextView 的商品中。我希望这能解决你的问题......

于 2012-05-16T09:10:16.010 回答