2011-06-06 90 views
3

我要一個* UITapGestureRecognize * R添加到我的的UITextView,因爲我要關閉「彈出窗口」,其中TextView的是,所以我想,該方法「隱藏」的彈出Popup類,當T * extView *被點擊時。我試了一下像下面,但它不工作由於某些原因:添加TapGestureRecognizer到的UITextView

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

我也不想爲它創建一個子類,因爲我則需要調用「父」 - 方法「隱藏」。

也許你現在是一個很好的解決方案。
預先感謝您。

回答

2

你不應該使用UITapGestureRecognizer,而是使用UITextFieldDelegate。

你可以閱讀一下:

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

您basicly需要將 UITextViewDelegate添加到您的.h文件中這樣的 -

@interface MyViewController : UIViewController<UITextViewDelegate> 

那麼你的控制器指定爲代理:

viewText.delegate =self; 

現在使用的委託方法之一,也許是:

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

    // Do what you need to do... 

} 

編輯

嗯,我能想到的2種額外的方法:

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

好運

+0

嗨,謝謝你的回答,但它不起作用。如果我點擊TextView,委託 - 方法textViewShouldBeginEditing沒有被調用(我添加了一個NSLog)。 有沒有像textViewTouchesEndes或東西的東西,對嗎? – 2011-06-06 08:36:38

+0

嘗試設置[textView seteditable:是]; ? – shannoga 2011-06-06 08:40:16

+0

嗯,不工作,不想要(不應該是可編輯的)。沒有辦法讓GestureRecognizer工作嗎? – 2011-06-06 08:57:51

0

你嘗試NSLog的展出方式?或者你甚至聲明和寫入方法「show」?它應該工作,這就是我在文本視圖中所做的。

PS不要忘記你的TextView添加後釋放你的手勢實例(GR):d

0

我得到這個工作的主要問題還,但我有一個愚蠢的問題,用戶交互在被關閉視覺編輯器。希望這可以幫助別人:)

相關問題