Add TapGestureRecognizer to UITextView

I want to add * UITapGestureRecognize * r to my UITextView, because I want to close the pop-up window where the TextView is located. So I want the "hide" method from the Popup class to be called when T * extView * is used. I tried this as follows, but for some reason it does not work:

UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(show)];
[gr setNumberOfTapsRequired:1];
[viewText addGestureRecognizer:gr];

I also do not want to subclass it, because then I will need to call the "parent" method of "hide".

Maybe you are a good solution to this problem right now.
Thank you in advance.

+3
source share
3 answers

You should not use UITapGestureRecognizer, but use UITextFieldDelegate.

:

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html%23//apple_ref/DOC/UID/TP40006897

UITextViewDelegate .h -

@interface MyViewController : UIViewController<UITextViewDelegate>

:

viewText.delegate =self;

, :

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{

   // Do what you need to do...

}

Edit

, :

  • textView UIView UITapGestureRecognizer.
  • :

     -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
         UITouch *touch = [touches anyObject];
         CGPoint location = [touch locationInView:textView];
    
         //Checks if the tap was inside the textview bounds
         if (CGRectContainsPoint(textView.bounds, location)){
             //do something
         }
     }
    

.

+2

NSLog show? ""? .

P.S (gr) : D

0

, , . , -:)

0
source

All Articles