I am trying to present an alert using swift. This is the code I used from viewDidLoad but nothing happens when the code is run. Can someone help?
var alert = UIAlertController(title: "test title",
message: "test message",
preferredStyle: .Alert)
self.presentViewController(alert, animated: true, completion:nil)
You must present any view controller after its parent view has been appeared . Put your presentation code to viewDidApear method.
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
var alert = UIAlertController(title: "test title",
message: "test message",
preferredStyle: .alert)
self.present(alert, animated: true, completion:nil)
}
Show AlerView in swift for ios8
func showAlertVIew(){
var alert = UIAlertController(title: "Info", message: "Welcome to Swift world.", preferredStyle: UIAlertControllerStyle.Alert)
let alertOKAction=UIAlertAction(title:"OK", style: UIAlertActionStyle.Default,handler: { action in
println("OK Button Pressed")
})
let alertCancelAction=UIAlertAction(title:"Cancel", style: UIAlertActionStyle.Destructive,handler: { action in
println("Cancel Button Pressed")
})
alert.addAction(alertOKAction)
alert.addAction(alertCancelAction)
self.presentViewController(alert, animated: true, completion: nil)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With