views:

5174

answers:

3

I'm trying to handle touches on a iPhone's UITextView. I successfully managed to handle taps and other touch events by creating a subclass of UIImageViews for example and implementing the touchesBegan method...however that doesn't work with the UITextView apparently :(

The UITextView has user interaction and multi touch enabled, just to be sure...no no joy. Anyone managed to handle this?

A: 

You need to assign the UITextView instance.delegate = self (assuming you want to take care of the events in the same controller)

And make sure to implement the UITextViewDelegate protocol in the interface... ex:

@interface myController : UIViewController <UITextViewDelegate>{
}

Then you can implement any of the following


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
- (BOOL)textViewShouldEndEditing:(UITextView *)textView;

- (void)textViewDidBeginEditing:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (void)textViewDidChange:(UITextView *)textView;

- (void)textViewDidChangeSelection:(UITextView *)textView;

dizy
This answer does not address the issue which is that a normal UITextView swallows touch events.
Harkonian
+6  A: 

UITextView (subclass of UIScrollView) includes a lot of event processing. It handles copy and paste and data detectors. That said, it is probably a bug that it does not pass unhandled events on.

There is a simple solution: you can subclass UITextView and impement your own touchesEnded (and other event handling messages) in your own versions, you should call[super touchesBegan:touches withEvent:event]; inside every touch handling method.

#import "MyTextView.h"  //MyTextView:UITextView
@implementation MyTextView

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        [super touchesBegan:touches withEvent:event];
    NSLog(@"touchesMoved");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"****touchesEnded");
    [self.nextResponder touchesEnded: touches withEvent:event]; 
    NSLog(@"****touchesEnded");
    [super touchesEnded:touches withEvent:event];
    NSLog(@"****touchesEnded");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[super touches... etc]; 
NSLog(@"touchesCancelled");
}
Can you elaborate on this? Does your touchesEnded method ever get called?
Ben Scheirman
edited to make clearer. Ben - the subclasses touchesEnded gets called and then effectively duplicates the event - once to the superclass (UITextView) which swallows the event and once to the next responder.
Roger Nolan
This still looks odd to me. Why are you calling the touchesBegan super on the touchesMoved? Wouldn't this mean that you would call touchesBegan over and over again and touchesMoved would never be handled by super?
Harkonian
A: 

I'm using a textview as a subview of a larger view. I need the user to be able to scroll the textview, but not edit it. I want to detect a single tap on the textview's superview, including on the textview itself.

Of course, I ran into the problem that the textview swallows up the touches that begin on it. Disabling user interaction would fix this, but then the user won't be able to scroll the textview.

My solution was to make the textview editable and use the textview's shouldBeginEditing delegate method to detect a tap in the textview. I simply return NO, thereby preventing editing, but now I know that the textview (and thus the superview) has been tapped. Between this method and the superview's touchesEnded method I have what I need.

I know that this won't work for people who want to get access to the actual touches, but if all you want to do is detect a tap, this approach works!

  • Charlie
CharlieMezak