Working with Alert in Swift Language – iOS 8 – Xcode 6

Alert View
Alert View

In iOS 8 the UIAlertView is deprecated. Now UIAlertController is a single class for creating and interacting with what we knew as UIAlertView.

We have to create alert as follows.

var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)

We can also create handler for handle the events on alert.

var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in
    switch action.style{
    case .Default:
        println("default")
        break

    case .Cancel:
        println("cancel")
        break

    case .Destructive:
        println("destructive")
        break
    }
    }))
self.presentViewController(alert, animated: true, completion: nil)

Happy Coding 🙂

For more iOS tutorials visit iTuts.