简体   繁体   English

touchesBegan没有回应

[英]touchesBegan doesn't respond

I'd like to use the touchesBegan method to know when the user is taping somewhen outside of a UITextField in my TableView. 我想使用touchesBegan方法来了解用户在我的TableView中的UITextField之外何时录制。

I've got a UIView that contains a TableView, and I have the following method : 我有一个包含TableView的UIView,我有以下方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSLog(@"See a tap gesture");
UITouch *touch = [touches anyObject];
if(touch.phase==UITouchPhaseBegan){
    //find first response view
    for (UIView *view in [self.view subviews]) {
        if ([view isFirstResponder]) {
            [view resignFirstResponder];
            break;
        }
    }
}


[super touchesBegan:touches withEvent:event];

} }

My self.view and my self.tableView have both the userInteractionEnabled set to YES, but for some reason, touchesBegan is never triggered. 我的self.view和我的self.tableView都将userInteractionEnabled设置为YES,但由于某种原因,touchesBegan从未被触发。 Any idea? 任何想法?

If your UIView contains your UITableView , then the table view is at the top of the responder chain and the touch events won't make it into your view. 如果您的UIView包含您的UITableView ,则表视图位于响应者链的顶部,触摸事件将不会进入您的视图。 But there might be a better way to do this. 但是可能有更好的方法来做到这一点。 What are you after? 你在追求什么?

UPDATE UPDATE

Implement UITextFieldDeletate 's textFieldShouldReturn: method to intercept a press of 'Return': 实现UITextFieldDeletatetextFieldShouldReturn:方法来拦截按下'Return':

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
   [textField resignFirstResponder];
   return YES;
}

Also, a UITableViewDelegate is a UIScrollViewDelegate, so you can hook into those methods to determine when the user interacts with the table view. 此外,UITableViewDelegate是一个UIScrollViewDelegate,因此您可以挂钩这些方法以确定用户何时与表视图交互。

Generally speaking I think you don't have to worry about dismissing the keyboard immediately when a user touches outside of it, especially if you have other text inputs on the same screen. 一般来说,我认为您不必担心当用户触摸键盘时立即解除键盘,特别是如果您在同一屏幕上有其他文本输入。

MOAR MOAR

Ok, fair enough, but things start to get complex when you intercept touch events via composite transparent views (these can get expensive as well), and so on. 好吧,公平地说,当你通过复合透明视图拦截触摸事件时,事情开始变得复杂(这些也会变得昂贵),等等。 And you never know the repercussions that will arise down the road, not only for the user but for you the programmer when you want to upgrade the app in the future. 并且您永远不会知道将来会产生的影响,不仅是对用户而且对于程序员,当您希望将来升级应用程序时。

Why not keep it simple? 为什么不保持简单? How about just a 'Done' UIBarButtonItem , or a little translucent UIToolbar that slides up on top of the keyboard/picker ala Mobile Safari? 怎么样只是一个'完成' UIBarButtonItem ,或者一个小的半透明的UIToolbar ,它可以在键盘/选择器上滑动移动Safari? These solutions are acceptable to the user, and can wind up making his life easier. 这些解决方案对用户来说是可以接受的,并且可以使他的生活更轻松。 They certainly make development easier by separating artifacts and functionality into modular units. 通过将工件和功能分离为模块化单元,它们确实使开发更容易。

One final note 最后一点说明

Using UITapGestureRecognizer I think will be difficult to get right in your situation. 使用UITapGestureRecognizer我认为很难在你的情况下做到正确。 I'm worried that any tap recognizer you add to the table view will prevent things like row selection or moving control to another UI element (text field, switch, etc). 我担心你添加到表格视图中的任何点击识别器都会阻止行选择或将控制移动到另一个UI元素(文本字段,开关等)。

Make sure you have set your object to process those touch events: 确保已设置对象以处理这些触摸事件:

[myButton addTarget:self action:@selector(buttonTouch:withEvent:) forControlEvents:UIControlEventTouchUpInside];

And then process the touch event in the method you declared: 然后在您声明的方法中处理触摸事件:

- (IBAction) buttonTouch:(id) sender withEvent:(UIEvent *) event

Updated for UIView (Assuming you already have an outlet created and wired up): 针对UIView进行了更新(假设您已经创建并连接了插座):

[myView addTarget:self action:@selector(myTouchEvent:withEvent:) forControlEvents:UIControlEventTouchUpInside];

And then: 接着:

- (IBAction) myTouchEvent:(id) sender withEvent:(UIEvent *) event

Now that I understand what you are trying to do (dismiss the keyboard when touching outside the textbox), there is a simple solution that works well with almost every scenario). 现在我明白了你要做的事情(在触摸文本框外面时解除键盘),有一个简单的解决方案几乎适用于所有场景)。

Step 1 - Create a button that is sized the same as the View that the TableViewController is placed in. (large button). 步骤1 - 创建一个与放置TableViewController的视图大小相同的按钮。(大按钮)。 Step 2 - Send that button to the back (Editor->Arrange->Send to Back) Step 3 - Wire up an IBAction to that button and call it dismissKeyboardButtonPressed (hint if you are not already using the assistant editor, you should be) Step 4 - Inside the IBAction method put (assuming your TextField is called myTextField): 步骤2 - 将该按钮发送到后面(编辑 - >排列 - >发送到后面)步骤3 - 将IBAction连接到该按钮并将其命名为dismissKeyboardButtonPressed(提示如果您尚未使用助理编辑器,则应该是)第4步 - 放入IBAction方法内部(假设您的TextField名为myTextField):

[myTextField resignFirstResponder];

Step 5 - Run it. 第5步 - 运行它。 Whenever you touch anywhere in the view outside of the textbox, it will dismiss the keyboard. 每当您触摸文本框外部视图中的任何位置时,它都会关闭键盘。

This is the method I use almost anytime I put a TextField on a view, and need to dismiss the KB when touching outside the TextField. 这几乎是我在视图上放置TextField的时候使用的方法,在触摸TextField外部时需要关闭KB。

Try adding a tap gesture recognizer to your table view. 尝试在表格视图中添加点按手势识别器。 That way you should be able to allow the normal behavior as well as capturing the tap event to do your extra stuff. 这样你就应该能够允许正常行为以及捕捉点击事件来做你额外的事情。

I've implemented similar functionality in one of my applications, I did use textFields rather than textViews, but it should still work. 我在我的一个应用程序中实现了类似的功能,我确实使用了textFields而不是textViews,但它仍然可以工作。 I used 我用了

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [textField reresignFirstResponder];
}

Without doing any checks, if the user taps the resignFirstResponder method gets called, but if the user taps on the textView, then that triggers it to become the first responder again, resulting in no visible change. 在不进行任何检查的情况下,如果用户点击resignFirstResponder方法被调用,但如果用户点击textView,则会触发它再次成为第一个响应者,导致没有明显的变化。 I've implemented this concept on an app with 9 textFields on a single view, and I have yet to experience problems. 我在一个应用程序上实现了这个概念,在一个视图上有9个textFields,我还没有遇到问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM