在UITableViewCell内部调用UIAlertController [英] Calling UIAlertController inside a UITableViewCell

查看:158
本文介绍了在UITableViewCell内部调用UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用函数时,我试图从UITtableViewCell内部调用UIAlertController.它给我一个错误,提示当前不可用.我了解它不在ViewController中.我正在寻找一种访问它的方法.

I am trying to call a UIAlertController from within my UITtableViewCell when my function is called. It gives me an error saying present is not available. I understand it's not within a ViewController. I am looking for an approach to access it.

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    let tapGestureShareImageView = UITapGestureRecognizer(target: self, action: #selector(self.shareImageTouchUpInside))
    shareImageView.addGestureRecognizer(tapGestureShareImageView)
    shareImageView.isUserInteractionEnabled = true
}

@objc func shareImageTouchUpInside() {
    showAction()
}

func showAction() {
    let alertController = UIAlertController(title: "Action Sheet", message: "What do you like to do", preferredStyle: .alert)

    let okButton = UIAlertAction(title: "Done", style: .default, handler: { (action) -> Void in
        print("Ok button tapped")
    })

    let deleteButton = UIAlertAction(title: "Skip", style: .destructive, handler: { (action) -> Void in
        print("Delete button tapped")
    })

    alertController.addAction(okButton)
    alertController.addAction(deleteButton)

    present(alertController, animated: true, completion: nil)
}

推荐答案

您可以尝试使用委托

protocol AlertShower{
    func showAlert(TableCustomCell)
}

class TableCustomCell: UITableViewCell {
    var delegate: AlertShower?

    @IBAction func showClicked(_ sender: UIButton) {
        self.delegate?.alertShower(sender:self)
    }
}

在VC中

class viewController: UIViewController, AlertShower {

    override func viewDidLoad() {
        super.viewDidLoad()

    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

         let cell = areaSettTable.dequeueReusableCell(withIdentifier:CellIdentifier1) as! TableCustomCell

         cell.delegate = self

         return cell
    }

    func showAlert(sender:TableCustomCell) {

      // show alert here

    }
}

这篇关于在UITableViewCell内部调用UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆