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

ios objective-c tableview

576 观看

2回复

4 作者的声誉

图片显示错误

我必须为此在按钮上添加警报,我想对按钮进行操作,所以我已经编写了此代码以打开警报

        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

作者: Husnain Ali 的来源 发布者: 2017 年 9 月 15 日

回应 2


1

67526 作者的声誉

决定

检查您的动作名称是否正确

[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 {
作者: Anbu.Karthik 发布者: 2017 年 9 月 15 日

0

66 作者的声誉

“ UIImageView的不可见@interface会清除选择器addTarget:action:forcontrolEvents” 发生此错误,因为addTarget方法是Button的,并且您试图在UIImageView上调用它。请检查自定义单元中的插座参考。引用附加到btnCommentOption不是Button,而是UIImageView。

作者: Aura Abhishek 发布者: 2017 年 9 月 15 日
32x32