tuz*_*ron 45 iphone cocoa-touch objective-c uitextview uikit
所以我有一个UITextView,我用它来允许用户提交一些文本.
我的问题是,我似乎无法弄清楚如何通过点击UITextView来允许用户"取消".
ohh*_*rob 107
简化tuzzolotron的答案:在xib中正确连接以下插座
IBOutlet UITextView *myTextView;
Run Code Online (Sandbox Code Playgroud)
在视图控制器中使用它:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([myTextView isFirstResponder] && [touch view] != myTextView) {
[myTextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)
在文本视图外部点击视图控制器的视图,将关闭第一个响应者,关闭键盘.
Sr.*_*hie 30
使用UITableViews,Searchbar和键盘时我经常使用的另一种方法是,我认为你可以将它应用到文本字段
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.tableView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO; // this prevents the gesture recognizers to 'block' touches
[gestureRecognizer release];
Run Code Online (Sandbox Code Playgroud)
然后,这是简单的回调函数
- (void)hideKeyboard {
[myTextField resignFirstResponder];
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你
在我看来:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// pass touches up to viewController
[self.nextResponder touchesBegan:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)
在我的viewcontroller中:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (keyboardShown && [touch view] != self.TextView)
{
[self.TextView resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
keyboardShown = YES;
}
Run Code Online (Sandbox Code Playgroud)