所以我有一个UITextView,我用它来允许用户提交一些文字。
我的问题是,我似乎无法弄清楚如何通过点击UITextView来让用户“取消”。
答案 0 :(得分:107)
简化tuzzolotron's answer: 以下插座在您的xib中正确连接
IBOutlet UITextView *myTextView;
在视图控制器中使用它:
- (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];
}
点击文本视图外部的视图控制器视图,将关闭第一个响应者,关闭键盘。
答案 1 :(得分: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];
然后,这是简单的回调函数
- (void)hideKeyboard {
[myTextField resignFirstResponder];
}
希望有所帮助
答案 2 :(得分:7)
在我看来:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// pass touches up to viewController
[self.nextResponder touchesBegan:touches withEvent:event];
}
在我的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;
}
答案 3 :(得分:3)
这是一篇很老的帖子,但是当我发现一些更简单的东西(当时可能没有它)时,我正在实施这个解决方案。
[self.myTextView endEditing:YES];
我使用的是UITableView,所以我把这一行放在了didSelectRowAtIndexPath:方法上。 到目前为止,这么好......
答案 4 :(得分:2)
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
var touch = event.allTouches()?.anyObject() as UITouch
if( textfield.isFirstResponder() && touch.view != textfield) {
textfield.resignFirstResponder()
NSLog("Resigning first responder since touches began outside")
}
super.touchesBegan(touches, withEvent: event)
}
哦,霍洛夫的回答对我也有用。这是简单的快速等价物。
答案 5 :(得分:2)
这是swift 3代码
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if textField.isFirstResponder && touch.view != textField {
textField.resignFirstResponder()
}
}
super.touchesBegan(touches, with: event)
}
答案 6 :(得分:1)
这是一个更好的解决方案,它不依赖于插座,并且可以与控制器中的任意数量的UITextField对象一起使用。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (![[touch view] isKindOfClass:[UITextField class]]) {
[self.view endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
答案 7 :(得分:1)
非常简单
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
答案 8 :(得分:0)
我想要一个适用于从任何视图中退出的版本,而不需要为每个视图提供特定的插座。这是我的解决方案,我将其放入我的标准容器视图中。
它正在使用MonoTouch C#,但显然如何将其转换为Objective-C
private void findAndResignFirstResponder(UIView node=null) {
if (node == null) { return; }
if (node.IsFirstResponder) {
node.ResignFirstResponder();
return;
}
foreach (UIView view in node.Subviews) {
findAndResignFirstResponder(node:view);
}
}
private bool haveDrag = false;
public override void TouchesEnded(NSSet touches, UIEvent evt) {
if (!haveDrag) {
UITouch touch = (UITouch)evt.AllTouches.AnyObject;
if (!touch.View.CanBecomeFirstResponder) {
this.findAndResignFirstResponder(this);
Console.WriteLine("resign");
}
}
base.TouchesEnded (touches, evt);
}
public override void TouchesMoved(NSSet touches, UIEvent evt) {
haveDrag = true;
base.TouchesMoved (touches, evt);
}
public override void TouchesBegan(NSSet touches, UIEvent evt) {
haveDrag = false;
base.TouchesBegan (touches, evt);
}
答案 9 :(得分:0)
我这样做......
在ViewController的@interface中添加对TextView的引用:
@property (weak, nonatomic) IBOutlet UITextView *yourTextView;
在ViewController的@implementation中重写此方法:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
if ([self.yourTextView isFirstResponder]) {
[self.yourTextView resignFirstResponder];
}
}