IOS开发控件视图day13:在TableView中的cell页面里的button单击响应弹窗如何实现

众所周知,ViewController中button的单击事件为:

[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

直接在该文件下面实现方法:

- (void):(UIButton *)btn
{
	UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"详细信息" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureAler = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
	UIAlertAction *cancelAler = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
	
    [aler addAction:sureAler];
    [aler addAction:cancelAler];
    [self presentViewController:aler animated:YES completion:nil];
}

但是在TableView的cell中则会报错,无法调用presentViewController,因为cell页面继承自UITableViewCell,且设置时需要区分是来自哪个按钮的请求,故必须设置tag值

1、首先,在该cell的头文件里面设置自定义代理(代理名字AlertClickDelegate自拟),写在@interface前面

@protocol AlertClickDelegate <NSObject>
-(void)clickTest:(NSInteger *)tag;
@end

然后在@interface里面声明代理

@property (nonatomic,weak)id<AlertClickDelegate>alertDelegate;

2、在cell页面的单击方法中设置代理转跳,并声明代理方法(此处代理方法为clickTest)

- (void)click:(UIButton *)btn
{
    if (self.alertDelegate != nil &&  [self.alertDelegate respondsToSelector:@selector(clickTest:)]) {
        [self.alertDelegate clickTest:btn.tag];
    }
}

3、在TableViewController里声明代理@interface后面写上
在cellForRow方法中,在声明并用到该自定义代理的cell中,设置代理cell.alertDelegate = self;
4、在TableViewController中,写入真正的单击事件具体方法

- (void)clickTest:(NSInteger *)tag
{
	if (tag == 0) {
		UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"详细信息" 								preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureAler = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
	UIAlertAction *cancelAler = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
	
    [aler addAction:sureAler];
    [aler addAction:cancelAler];
    [self presentViewController:aler animated:YES completion:nil];
	}else if(tag == 1){
		UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"详细信息" 								preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *sureAler = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
	UIAlertAction *cancelAler = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
	
    [aler addAction:sureAler];
    [aler addAction:cancelAler];
    [self presentViewController:aler animated:YES completion:nil];
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值