views:

2189

answers:

2

I have a UITextView that I want to detect a single tap for.

It looks like I would be fine with simply overriding touchesEnded:withEvent and checking [[touches anyObject] tapCount] == 1 , however this event doesn't even fire.

If I override the 4 events like this:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    NSLog(@"touchesBegan (tapCount:%d)", touch.tapCount);
    [super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touches moved");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    NSLog(@"touchesEnded (tapCount:%d)", touch.tapCount);
        [super touchesEnded:touches withEvent:event];
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touches cancelled");
}

I get output like this:

> touchesBegan (tapCount:1)
> touchesCancelled 
> touchesBegan (tapCount:1) 
> touches moved 
> touches moved
> touches moved 
> touchesCancelled

It seems I never get the touchesEnded event.

Any ideas?

A: 

You can turn off Cut/Copy/Paste by overriding the canPerformAction:withSender: method, so you could just return NO for all the actions you don't want to permit.

See the UIResponder documentation...

Hopefully that will stop your touches from being eaten.

David Maymudes
I tried overriding this method and ran into crashes. I returned NO for @selector(select:) and @selector(selectAll:) but it still allowed the selecting.
Ben Scheirman
Ok, fixed the crashes (I had forgotten to pass the rest over to super) anyway, it seems to have no effect. I can still copy text.
Ben Scheirman
The documentation says that this method will also be called further up the responder chain, so it's possible you'll also need to override it in the containing views...
David Maymudes
+1  A: 

Update: I ended up using the technique here: https://devforums.apple.com/message/94569#94569

I'm not sure if this is a bug or not, but the UITextView does need to utilize the touch events to do the popup menu for copy & paste for 3.0, so that might explain why it swallows this event.

Pretty lame if you ask me.

Update: I blogged about this here: http://flux88.com/blog/detecting-a-tap-on-a-uitextview/

Ben Scheirman