2017-09-15 52 views
0

image showing error按鈕在表視圖目標添加動作C

我不得不按鈕添加報警爲了這個,我想對按鈕的動作做洙我已經writen這個代碼來打開警報

 cell.btnCommentOption.tag = indexPath.row; 
     if([Boomerang sharedManager].currentUser.user_id != comment.user.user_id){ 
      cell.btnCommentOption.hidden = YES; 
      [cell.btnCommentOption addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; 
     } 

和按鈕動作是

- (IBAction)btnCommentOptionsTapped:(UIButton*)sender { 
    UIAlertController * alert = [UIAlertController 
           alertControllerWithTitle:@"Share" 
           message:@"" 
           preferredStyle:UIAlertControllerStyleActionSheet]; 



    UIAlertAction* sharefeed = [UIAlertAction 
           actionWithTitle:@"Share feed" 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * action) { 
            [alert dismissViewControllerAnimated:YES completion:^{}]; 
           }]; 

    UIAlertAction* report = [UIAlertAction 
          actionWithTitle:@"Report" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) { 
            [alert dismissViewControllerAnimated:YES completion:^{}]; 
          }]; 

    UIAlertAction* cancel = [UIAlertAction 
          actionWithTitle:@"Cancel" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) { 
           [alert dismissViewControllerAnimated:YES completion:^{}]; 
          }]; 

    [alert addAction:sharefeed]; 
    [alert addAction:report]; 
    [alert addAction:cancel]; 

    [self presentViewController:alert animated:YES completion:nil] 
} 

我有錯誤,當我添加動作按鈕好心幫助我的錯誤是No invisible @interface for UIImageView declears the selectors addTarget:action:forcontrolEvents

+0

什麼錯誤 –

+0

這是正確的'cell.btnCommentOption.hidden = YES;'或這一個是正確的'cell.btnCommentOption。隱藏= NO;'如果你在打電話懇請更新條件 –

+0

裏面有一個看起來 –

回答

1

檢查你的動作名稱iscorrect或不

[cell.btnCommentOption addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; 

[cell.btnCommentOption addTarget:self action:@selector(btnCommentOptionsTapped:) forControlEvents:UIControlEventTouchUpInside]; 

更新答案

if([Boomerang sharedManager].currentUser.user_id == comment.user.user_id){ 
     cell.btnCommentOption.hidden = NO; 
     [cell.btnCommentOption addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside]; 
    }else 
    { 
     cell.btnCommentOption.hidden = YES; 
    } 

,並致電按鈕行動

- (IBAction)didTapButton:(UIButton*)sender { 

- (IBAction)btnCommentOptionsTapped:(UIButton*)sender { 

最終Upate

「爲的UIImageView沒有隱形@interface declears的選擇addTarget:動作:forcontrolEvents」

錯誤說cell.btnCommentOption is the UIimageview not a UIButton

UIImageView不是UIControl,所以它沒有addTarget:action:forControlEvents方法作爲其接口的一部分。您可以使用手勢識別器。

在你cellForRowAtIndexPath方法中添加以下代碼

cell. btnCommentOption.userInteractionEnabled = YES; 
cell. btnCommentOption.tag = indexPath.row; 
cell.btnCommentOption.hidden = YES; 
if([Boomerang sharedManager].currentUser.user_id == comment.user.user_id){ 
     cell.btnCommentOption.hidden = NO; 
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnCommentOptionsTapped:)]; 
tap.numberOfTapsRequired = 1; 
[cell.btnCommentOption addGestureRecognizer:tap]; 

    } 

,並調用方法

- (void)btnCommentOptionsTapped:(UITapGestureRecognizer *)sender { 
+0

這不是一個問題,我已經做了很多次,並有相同的錯誤給最後的我的問題 –

+0

是否爲imageview添加了'addTarget:action:forcontrolEvents',如果是,請將'addTarget:action:forcontrolEvents'改爲Tapgesture, –

+0

@HusnainAli - cell.btnCommentOption是UIimageView或UIButton –

0

「爲的UIImageView沒有隱形@interface declears的選擇addTarget:動作:forcontrolEvents」 發生此錯誤的原因是addTarget方法屬於Button,並且您試圖在UIImageView上調用它。請檢查您的自定義單元中的出口參考。附加到btnCommentOption的參考不是Button而是UIImageView。