众所周知,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];
}
}
原创:https://www.panoramacn.com
源码网提供WordPress源码,帝国CMS源码discuz源码,微信小程序,小说源码,杰奇源码,thinkphp源码,ecshop模板源码,微擎模板源码,dede源码,织梦源码等。专业搭建小说网站,小说程序,杰奇系列,微信小说系列,app系列小说
免责声明,若由于商用引起版权纠纷,一切责任均由使用者承担。
您必须遵守我们的协议,如果您下载了该资源行为将被视为对《免责声明》全部内容的认可-> 联系客服 投诉资源www.panoramacn.com资源全部来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。 敬请谅解! 侵权删帖/违法举报/投稿等事物联系邮箱:2640602276@qq.com
关注我们小说电影免费看关注我们,获取更多的全网素材资源,有趣有料!120000+人已关注
评论抢沙发