3

我想向我的UITextView添加一个 *UITapGestureRecognize*r ,因为我想关闭TextView所在的“Popup” 。所以我希望在 T*extView 时调用Popup类的方法“hide” * 被点击。我尝试了以下方法,但由于某种原因它不起作用:

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

我也不想为它创建一个子类,因为我需要调用“父”方法“隐藏”。

也许您现在是该问题的一个很好的解决方案。
先感谢您。

4

3 回答 3

2

您不应该使用 UITapGestureRecognizer,而是使用 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...

}

编辑

好吧,我可以考虑另外两种方法:

  1. 您可以将 textView 包装在 UIView 中,并将 UITapGestureRecognizer 添加到视图中。
  2. 您可以使用 :

     -(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
         }
     }
    

祝你好运

于 2011-06-06T08:13:52.227 回答
0

您是否尝试过 NSLog on show 方法?还是您甚至声明并编写了“show”方法?它应该可以工作,这就是我在文本视图中所做的。

PS在添加textview后不要忘记释放手势实例(gr):D

于 2011-07-27T20:22:14.170 回答
0

我也遇到了重大问题,但我有一个愚蠢的问题,在可视化编辑器中关闭了用户交互。希望这可以帮助某人:)

于 2011-11-22T02:37:03.397 回答