Confused
Confused

Reputation: 3926

touchesBegan method not called in UITableViewController class in iPhone

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?

Upvotes: 21

Views: 26964

Answers (5)

arled
arled

Reputation: 2690

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

Upvotes: 2

Or Arbel
Or Arbel

Reputation: 2975

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.

Upvotes: 8

Miguel Ehrstrand
Miguel Ehrstrand

Reputation: 71

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

Upvotes: 5

Steph Sharp
Steph Sharp

Reputation: 11682

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.

Upvotes: 24

Balakrishnan Mca
Balakrishnan Mca

Reputation: 747

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

Upvotes: 4

Related Questions