PDA

View Full Version : Trigger an action tapping a textview




Alhazred
Jul 13, 2011, 02:23 AM
Is there a way to trigger an action when a user taps inside a textview area?
I know that I could put an invisible button on it, but that makes impossible to scroll the text.



jnoxx
Jul 13, 2011, 03:00 AM
Quite easy..
You have delegate methods on your UITextView (UITextViewDelegate).
TextviewDidBeginEditing (or something similair, i'll leave it up to you to find that out.).
THen trigger a action there..
for eg.
[self doMethod1];
or do a performSelector after Delay 4.

You're smart enough to find the rest out.

Alhazred
Jul 13, 2011, 05:02 AM
Here what I've done, but with no luck:
int the nib file I've linked the UIView as delegate for the textview

I've declared my view conltroller in this way
@interface PickItemDetailViewController : UIViewController <UITextViewDelegate> {

then in the .m file I've put
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(@"textViewDidBeginEditing");
}

but nothing appears inside the console.

Do I miss anything?

jnoxx
Jul 13, 2011, 05:20 AM
You don't need to link the UIView as a delegate, because, it's the textView that you need as delegate..
you can set it in your code too. if you added the delegate (in your header file).

You can just do this

myTextView.delegate = self (in your viewDidLoad).

And then it should be triggered (if the correct myTextView is connected via IB), in other words -->
IBOutlet myTextView *myTextView;

Alhazred
Jul 13, 2011, 05:34 AM
Thanks, it works :)