2017-09-15 53 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。