gpt4 book ai didi

ios - 在 TableView objective-c 中的按钮上添加操作

转载 作者:行者123 更新时间:2023-11-29 00:12:01 25 4
gpt4 key购买 nike

image showing error

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

        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]
}

当我向按钮添加操作时出现错误,请帮我解决错误:UIImageView 没有不可见的 @interface 清除选择器 addTarget:action:forcontrolEvents

最佳答案

检查你的 Action 名称是否正确

[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是UIimageview而不是UIButton

UIImageView is not a UIControl so it doesn't have the addTarget:action:forControlEvents method as part of its interface. You can use a gesture recognizer instead.

在您的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 {

关于ios - 在 TableView objective-c 中的按钮上添加操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com