[英]How to present an alert with UIAlertController
I am trying to present an alert using swift.我正在尝试使用 swift 发出警报。 This is the code I used from viewDidLoad but nothing happens when the code is run.
这是我从 viewDidLoad 使用的代码,但在运行代码时没有任何反应。 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.
将您的演示文稿代码放入 viewDidApear 方法。
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在 ios8 中快速显示 AlerView
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.