在失去对UITextView的焦点时隐藏键盘

所以我有一个UITextView,我用它来允许用户提交一些文本。

我的问题是,我似乎无法弄清楚如何允许用户通过从UITextView中删除“取消”。

简化tuzzolotron的答案 :以下sockets正确连接在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]; } 

在文本视图之外的视图控制器视图上点击,将会退出第一个响应者,closures键盘。

我使用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]; 

然后,这是简单的callback函数

 - (void)hideKeyboard { [myTextField resignFirstResponder]; } 

希望能帮助到你

在我看来:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; // pass touches up to viewController [self.nextResponder touchesBegan:touches withEvent:event]; } 

在我的视图控制器中:

 - (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; } 

这是一个旧的post,但是当我发现一些更简单的东西时(也许它当时不可用),我正在实施这个解决scheme。

[self.myTextView endEditing:YES];

我正在使用UITableView,所以我把这一行放在didSelectRowAtIndexPath:方法。 到现在为止还挺好…

这是一个更好的解决scheme,不依赖于sockets,并将与您的控制器中的任意数量的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]; } 

好简单

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } 
 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) } 

ohhorob的回答也适用于我。 这是简单的快速等值。

这是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) } 

我想要一个这样的版本从任何看法的工作辞职,而不需要每个人的具体渠道..这是我的解决scheme,我把它放到我的标准容器视图之一..

它使用的是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); } 

我的方式做到这一点…

  • 在ViewController的@interface中添加对TextView的引用:

     @property (weak, nonatomic) IBOutlet UITextView *yourTextView; 
  • 在你的ViewController的@implementation中重写这个方法:

     - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { if ([self.yourTextView isFirstResponder]) { [self.yourTextView resignFirstResponder]; } }