在表视图目标c中的按钮上添加操作

时间:2017-09-15 05:11:58

标签: ios objective-c tableview

image showing error

我必须在按钮上添加警报为此我希望按钮上的操作做soo我已经写了这段代码来打开警报

        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

2 个答案:

答案 0 :(得分:1)

检查你的行动名称是否正确

[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 {

最终更新

“UIImageView没有隐形的@interface会忽略选择器addTarget:action: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 {

答案 1 :(得分:0)

" UIImageView没有隐形的@interface会忽略选择器addTarget:action:forcontrolEvents" 发生此错误,因为addTarget方法是Button,并且您尝试在UIImageView上调用它。请检查自定义单元格中的插座参考。引用附加到btnCommentOption不是Button而是UIImageView。