-1

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...

Vic W.
  • 817
  • 1
  • 7
  • 9
  • Simply, UITableViewCell is not a view controller. – El Tomato Mar 14 '18 at 22:40
  • Possible duplicate of [Calling UIAlertController inside a UITableViewCell](https://stackoverflow.com/questions/49199716/calling-uialertcontroller-inside-a-uitableviewcell) – El Tomato Mar 14 '18 at 22:42

2 Answers2

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

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

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

Angel
  • 402
  • 3
  • 8