iPhone 中的 UITableViewController 类中未调用 TouchBegan 方法

发布于 2024-12-25 18:59:00 字数 174 浏览 3 评论 0原文

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

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

How can I use touchesBegan: withEvent: method in UITableViewController class?

UITableViewController is a subclass of UIViewController class. So why the method does not works in UITableViewController?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回忆躺在深渊里 2025-01-01 18:59:00

我遇到了类似的问题,并找到了一种不涉及 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...
}

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

I had a similar problem, and found a different approach that doesn't involve subclassing UITableView. Another way to do this is to add a gesture recognizer to the UITableViewController's view.

I put this code in the UITableViewController's viewDidLoad:

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

And implemented the event handler:

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

I know this solution doesnt use touchesBegan, but I found it was a simple solution to the same problem.

我们只是彼此的过ke 2025-01-01 18:59:00

TouchBegan 除了是 UIViewController 方法之外也是 UIView 方法。

要覆盖它,您需要子类化 UIView 或 UITableView 而不是控制器。

touchesBegan is a also UIView method besides being a UIViewController method.

to override it you need to subclass UIView or UITableView and not the controllers.

再见回来 2025-01-01 18:59:00

这是一个对我有用的 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];
}

Here is a UITableView subclass solution that worked for me. Make a subclass of UITableView and override hitTest:withEvent: as below:

- (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];
}
冷血 2025-01-01 18:59:00

TouchBegan 是 UIView 和 UITableViewCell 方法,而不是 UIViewController 和 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);


}

祝你有美好的一天

touchesBegan is a UIView and UITableViewCell method rather than a UIViewController & UITableViewController method.
so you may create the the custom class for UITableViewCell it recognize the touch event and touch delegate it is working for me.

  //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);


}

have a good day

怼怹恏 2025-01-01 18:59:00

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.
}

IN SWIFT - I came across this question while searching for a Swift 2 solution. The answer posted by @Steph Sharp helped me work out the the problem in Swift so I though I'de post it on here. Here you go:

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

Function

func handleTap(recognizer: UITapGestureRecognizer) {
    // Do your thing.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文