Tuesday, October 07, 2014

iOS 8 - Display an alert

The UIAlertView class is deprecated in iOS8. In place of it is the more adaptive UIAlertController class. Here is a quick example of how to display an alert in iOS 8:

    func displayAlert(title:String, content:String) {
        var alertController = UIAlertController(
            title: title,
            message: content,
            preferredStyle: UIAlertControllerStyle.Alert)
        
        var okAction = UIAlertAction(
            title: "OK", style: UIAlertActionStyle.Default) {
                (action) -> Void in
                println("Tapped on OK")
        }
        
        alertController.addAction(okAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }

To display an alert, you can call the displayAlert() function like this:

self.displayAlert("Hello", content: "Hello, iOS 8")

The above will display an alert like the following:

No comments: