touchesBegan method not called in UITableViewController class in iPhone
Asked Answered
B

5

21

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?

Bunde answered 9/1, 2012 at 7:34 Comment(3)
Where you want to use this method in tableviewcontroller?Litigable
already answered at this #5383183Chin
subclass UITableView and implement touchesBegan there. It will work painlessly.Assets
U
8

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.

Undue answered 9/1, 2012 at 10:6 Comment(1)
The apple docs say UITableViewController is a UIViewController which is a UIResponder. UIResponder has touchesBegan:withEventGuib
A
24

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.

Alkalimeter answered 19/2, 2013 at 23:47 Comment(1)
Doesn't this only trigger once you lift your finger off the screen?Wages
U
8

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.

Undue answered 9/1, 2012 at 10:6 Comment(1)
The apple docs say UITableViewController is a UIViewController which is a UIResponder. UIResponder has touchesBegan:withEventGuib
T
5

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];
}
Teheran answered 15/11, 2013 at 14:51 Comment(0)
H
4

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

Histidine answered 9/1, 2012 at 10:30 Comment(3)
this will not work as tableview will absorb the touch and it will never reach the touchesBeganAssets
before comment and vote please check the code if it is working or not, TableViewCell is the sub class of UIView so if UIView response for the touch then it should be for TableViewCell Justify!!!Histidine
This code will never work as explained by comments from @balakrishnan. Both of their comments are completely right. I even tried the code for curiosity and yes it is not workingAssai
R
2

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.
}
Retrench answered 31/10, 2015 at 0:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.