Question

I am using swift and have a class called ViewController() that is linked to my storyboard. I am trying to get an alert view to display from another class. I am using the following code however it will not open an alert box. Any Ideas?

public class SomeClass {

        func showAlert(title:String, body:String) {
            var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))

}
Was it helpful?

Solution

    func showAlert(title:String, body:String) {
        var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))        
        var vc: ViewController = ViewController()
        vc.presentViewController(alert, animated: true, completion: nil)
    }

OTHER TIPS

You have to call presentViewController on your UIViewController and pass in your UIAlertViewController.

In UIViewController:

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

alert being your UIAlertViewController.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top