【问题标题】:touchesBegan method not called in UITableViewController class in iPhone在 iPhone 的 UITableViewController 类中未调用 touchesBegan 方法
【发布时间】:2012-01-09 07:34:11
【问题描述】:

如何在 UITableViewController 类中使用touchesBegan: withEvent: 方法?

UITableViewController 是 UIViewController 类的子类。那么为什么该方法在 UITableViewController 中不起作用呢?

【问题讨论】:

  • 你想在tableviewcontroller哪里使用这个方法?
  • 已经在这个stackoverflow.com/questions/5382683/…回答了
  • 子类 UITableView 并在那里实现 touchesBegan。它会无痛工作。

标签: iphone uitableview touchesbegan


【解决方案1】:

我遇到了类似的问题,并找到了一种不涉及子类化 UITableView 的不同方法。另一种方法是在 UITableViewController 的视图中添加手势识别器。

我把这段代码放在 UITableViewController 的 viewDidLoad 中:

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.view addGestureRecognizer:tap];

并实现了事件处理程序:

- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
    // your code goes here...
}

我知道这个解决方案不使用 touchesBegan,但我发现它是解决同一问题的简单解决方案。

【讨论】:

  • 这不是只有在您将手指从屏幕上移开后才会触发吗?
【解决方案2】:

touchesBegan 除了是 UIViewController 方法之外,也是 UIView 方法。

要覆盖它,您需要继承 UIView 或 UITableView 而不是控制器。

【讨论】:

  • 苹果文档说 UITableViewController 是一个 UIViewController,它是一个 UIResponder。 UIResponder 有 touchesBegan:withEvent
【解决方案3】:

这是一个对我有用的 UITableView 子类解决方案。创建 UITableView 的子类并覆盖 hitTest:withEvent: 如下:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    static UIEvent *e = nil;

    if (e != nil && e == event) {
        e = nil;
        return [super hitTest:point withEvent:event];
    }

    e = event;

    if (event.type == UIEventTypeTouches) {
        NSSet *touches = [event touchesForView:self];
        UITouch *touch = [touches anyObject];
        if (touch.phase == UITouchPhaseBegan) {
            NSLog(@"Touches began");
        }
    }
    return [super hitTest:point withEvent:event];
}

【讨论】:

    【解决方案4】:

    touchesBegan 是 UIView 和 UITableViewCell 方法,而不是 UIViewController 和 UITableViewController 方法。 因此,您可以为 UITableViewCell 创建自定义类,它可以识别触摸事件并为我工作的触摸委托。

      //TableViewCell.h
    
    #import <UIKit/UIKit.h>
    
    @class Util;
    
    @interface TableViewCell : UITableViewCell {
    
    }
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
    
    @end
       //TableViewCell.m
    #import "TableViewCell.h"
    @implementation TableViewCell
    
    -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier      format:(NSString*)ec_format{
    
        if (self) {
            self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
       }
    
       return self;
    }
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
     {
    //you receive touch here 
        NSLog(@"Category Touch %@",self.frame);
    
    
    }
    

    祝你有美好的一天

    【讨论】:

    • 这不起作用,因为 tableview 会吸收触摸并且永远不会到达 touchesBegan
    • 在评论和投票前请检查代码是否正常工作,TableViewCell 是 UIView 的子类,所以如果 UIView 响应触摸,那么它应该是 TableViewCell Justify!!!
    • 此代码永远不会像 @balakrishnan 的 cmets 所解释的那样工作。他们的两个cmets都是完全正确的。我什至出于好奇尝试了代码,是的,它不起作用
    【解决方案5】:

    IN SWIFT - 我在寻找 Swift 2 解决方案时遇到了这个问题。 @Steph Sharp 发布的答案帮助我解决了 Swift 中的问题,所以我将其发布在这里。给你:

    class CalcOneTableViewController: UITableViewController {
          override func viewDidLoad() {
             super.viewDidLoad()
             let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
             self.view.addGestureRecognizer(tap)
    }
    

    功能

    func handleTap(recognizer: UITapGestureRecognizer) {
        // Do your thing.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多