2012-08-08 67 views
1

我有一個奇怪的問題與UIGestureRecognizerUIGestureRecognizer目標問題

我已經創建了一個類,其中我宣佈手勢識別,並把自我作爲目標

-(id)initWithTextView:(UITextView*)theTextView withDelegate:(id<WordSelectionDelegate>)theDelegate 
{ 
    if (self = [super init]) 
    { 
     delegate = theDelegate; 
     textView = theTextView; 
     // init long press gesture to detect pressing on text elements 
     UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressFromSender:)]; 
     [textView addGestureRecognizer:longPressGesture]; 
    } 
    return self; 
} 

但訣竅是當我實際上做出長按手勢,我有下一個錯誤:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableAttributedString handleLongPressFromSender:]: unrecognized selector sent to instance 0x76227b0'

爲什麼消息自我去串?

+1

上述代碼沒有任何問題。你必須擴大你的搜索範圍。在'handleLongPressFromSender'中放置一個斷點並確保它被調用(如果沒有,是否出現'WordSelection'對象超出範圍並被釋放給你?或者你輸入了錯誤的方法名嗎?或者只執行那個方法一個參數沒有正確的參數類型?)。如果它被調用,那麼再次擴大搜索範圍,看看你的'WordSelectionDelegate'定義,並確保你沒有問題。 – Rob 2012-08-08 16:28:22

回答

2

順便說一句,這個問題無疑是具有handleLongPressFromSender實例方法(即你與initWithTextView初始化的對象)的對象由UILongPressGestureRecognizer被調用時掉落的範圍。您需要檢查該對象的範圍。

例如,假設這個類的名字是MyTextViewHandler,想象一下你曾經對自己有這樣的事情有些視圖控制器等viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // do a bunch of initialization 

    MyTextViewHandler *textViewDelegate = [[MyTextViewHandler alloc] initWithTextView:self.textview withDelegate:self]; 
} 

如果您在ARC項目做了這樣的事情,你」 d得到你描述的崩潰(因爲textViewDelegate對象是viewDidLoad的本地對象,並且在該方法結束時將會超出範圍)。如果你讓這個委託處理程序類成爲視圖控制器的一個實例變量(或屬性),那麼這個問題就會消失。