UITableView cell invokes UIAlertController popup …“present” error

若如初见. 提交于 2019-12-13 07:56:27

问题


I have a tableview definition in which I am attempting to invoke an UIAlertController popup. I installed a button in the prototype tableView cell, when the button is touched, an IBAction handles the event. The problem is that the compiler won't let me.

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

Generates compiler error: "Use of unresolved identifier 'present'

Here is the code:

class allListsCell: UITableViewCell {

    @IBOutlet var cellLable:       UIView!
    @IBOutlet var cellSelected:    UILabel!
    var colorIndex = Int()        

    @IBAction func cellMarkButton(_ sender: UIButton, forEvent event: UIEvent) {
        if  colors[self.colorIndex].selected == false {
            colors[self.colorIndex].selected = true
            cellSelected.text = "•"

            let alertController = UIAlertController(title: "???", message: "alertA", preferredStyle: .alert)

            let OKAction = UIAlertAction(title: "dismiss", style: .default) { (action:UIAlertAction!) in
                print("Sand: you have pressed the Dismiss button");
            }
            alertController.addAction(OKAction)

            present(alertController, animated: true, completion: nil) // ERROR

        } else {
            colors[self.colorIndex].selected = false
            cellSelected.text = ""
        }
    }

If I comment that one line, the app runs correctly for each cell...


回答1:


You can't call present AlertController inside a tableView cell , it needs a subclass of UIViewController or other equivalent one , you should use a delegate or some sort of notification to handle that , see my answer here for the same problem AlertControllerCallInsideCell

Edit : Form Docs , it's an instance method inside UIViewController . so it can't be called inside any other class of other type (UITableViewCell) in your case




回答2:


It is not possible to call the "present" method from a TableViewCell, I recommend having a function in the main controller to show your UIAlertController.

Using this code you can instantiate the parent driver and execute any available function:

extension UIView {
    var parentViewController: UIViewController? {
        var parentResponder: UIResponder? = self
        while parentResponder != nil {
            parentResponder = parentResponder!.next
            if let viewController = parentResponder as? UIViewController {
                return viewController
            }
        }
        return nil
    }
}

//UITableViewCell
if let controller = self.parentViewController as? YourController
{
    controller.showAlert()
}

Here is an example of its use with a CollectionViewCell: https://github.com/AngelH2/CollectionViewCell-Comunication/tree/master/CollectionCellAction



来源:https://stackoverflow.com/questions/49287607/uitableview-cell-invokes-uialertcontroller-popup-present-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!