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?
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?
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.
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.
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.
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];
}
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
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.
}
© 2022 - 2024 — McMap. All rights reserved.