[英]How to present an alert with UIAlertController
我正在嘗試使用 swift 發出警報。 這是我從 viewDidLoad 使用的代碼,但在運行代碼時沒有任何反應。 有人可以幫忙嗎?
var alert = UIAlertController(title: "test title",
message: "test message",
preferredStyle: .Alert)
self.presentViewController(alert, animated: true, completion:nil)
您必須在其父視圖出現后呈現任何視圖控制器。 將您的演示文稿代碼放入 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)
}
在 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.