views:

337

answers:

2

I have a UITableViewController and want to detect touches.

Basically, I want the user to be able to hold down a touch for 3 seconds, when they let go I want to pop up a small view with a few options for the table.

I've tried this...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 self.lastTouch = [event timestamp];
 NSLog(@"TLC.touchesBegan:touchBeginEndInterval %f", self.lastTouch);
}  

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 NSTimeInterval touchBeginEndInterval = [event timestamp] - self.lastTouch
 NSLog(@"TLC.touchesEnded:touchBeginEndInterval %f %f", self.lastTouch, touchBeginEndInterval);
}

And it isn't picking up my touches at all...

Any ideas about the best way to implement this?

A: 

TableViews have sort of a "touch" delegate method. Implement this method for your tableView delegate:

// What happens when a row is touched
- (void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}

You shouldn't make them press for 3 seconds... just use the standard.

Andrew Johnson
I'm already using that for other things.I want them to hold down for a few seconds before I pop up another view. I thought about using the editingStyleForRowAtIndexPath method but wanted to leave that as Delete (which it currently is).Any other ideas?
iphone_developer
You should use the little blue button inside the tableViewCell. That is sort of the standard way to have more interaction with the cell. http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/MobileHIG/art/UIButtonTypeDetailDisclosure.jpg. This button is listed in the HIG: http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/MobileHIG/SystemProvided/SystemProvided.html#//apple_ref/doc/uid/TP40006556-CH15-SW2
Andrew Johnson
A: 

Assuming that code comes from your UITableViewController subclass ... UIViewControllers don't receive methods like touchesBegan:withEvent. Only UIResponders like UIView an its subclasses like UITableView do. So you are trying to get the touches in the wrong place.

What are you trying to accomplish? Do you want to respond to touches in the UITableView or in a UITableViewCell? In case of the latter, you can deal with the touches in your custom cell implementation.

St3fan
UIViewController inherits from UIResponder, so can receive touches.See http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
Mark Beaton