0

How can I use an UIAlertController inside a TableViewCell?

Gives me an error does not have a member called "presentViewController".

My TableViewController is named "OrdensCompraTableViewController"

My function:

@IBAction func telefonarCliente(sender: AnyObject) {

    var alert = UIAlertController(title: "Fotos em falta!", message: "Tem de introduzir 6 fotos.", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))


    self.presentViewController(alert, animated: true, completion: nil)

    if let phoneCallURL:NSURL = NSURL(string: "tel://") {
        let application:UIApplication = UIApplication.sharedApplication()
        if (application.canOpenURL(phoneCallURL)) {
            application.openURL(phoneCallURL);
        }
    }
}
Renato Pimpão
  • 271
  • 3
  • 17

2 Answers2

1

You can't present a UIViewController from a UITableViewCell (it's not a view controller). You have two options:

1 - Assign a delegate to the UITableViewCell pointing back to its parent view controller and preset the alert on the view controller.

2 - Present the alert on the application's root view controller:

UIApplication.sharedApplication().delegate?.window??.rootViewController?.presentViewController(alert, animated: true, completion: nil)
JAL
  • 41,701
  • 23
  • 172
  • 300
  • Ok, but I have to have an alert to make a phone call when the user clicks the button inside the tableviewcell... How can I do that if i'm calling the alert from the parent view? – Renato Pimpão Aug 26 '15 at 20:23
  • Did you try it? What happens? Is it what you expect? What do you want? Do you want the button to show an alert, and then the alert action to start a phone call? You need to add the phone call functionality to an alert action. – JAL Aug 26 '15 at 20:25
1

Use telprompt://123456789 instead of creating a UIAlertController to prompt the user before dialing the phone number.

JAL
  • 41,701
  • 23
  • 172
  • 300
Yariv Nissim
  • 13,273
  • 1
  • 38
  • 44