所以我有一个 UITextView 用来允许用户提交一些文本。
我的问题是,我似乎无法弄清楚如何通过点击 UITextView 来允许用户“取消”。
所以我有一个 UITextView 用来允许用户提交一些文本。
我的问题是,我似乎无法弄清楚如何通过点击 UITextView 来允许用户“取消”。
简化tuzzolotron 的答案:以下插座在您的 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];
}
在文本视图之外点击视图控制器的视图,将使第一响应者辞职,关闭键盘。
我在使用 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];
}
希望能帮助到你
在我看来:
- (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;
}
这是一篇旧文章,但是当我发现一些更简单的东西时(也许当时不可用),我正在实施这个解决方案。
[self.myTextView endEditing:YES];
我正在使用 UITableView,所以我将这一行放在 didSelectRowAtIndexPath: 方法上。到现在为止还挺好...
这是一个更好的解决方案,它不依赖于插座,并且可以与控制器中任意数量的 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];
}
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 等价物。
这是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)
}
好简单
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
我想要一个可以从任何视图中退出的版本,而不需要为每个视图提供特定的出口。这是我的解决方案,我将其放入我的一个标准容器视图中。
它使用的是 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];
}
}