如何在Swift中创建UIAlertView? [英] How would I create a UIAlertView in Swift?
问题描述
我一直在努力在Swift中创建一个UIAlertView,但由于某种原因,我无法得到正确的语句,因为我得到这个错误:
I have been working to create a UIAlertView in Swift, but for some reason I can't get the statement right because I'm getting this error:
无法为接受所提供的
参数的'init'找到重载
Could not find an overload for 'init' that accepts the supplied arguments
我写了:
let button2Alert: UIAlertView = UIAlertView(title: "Title", message: "message",
delegate: self, cancelButtonTitle: "Ok", otherButtonTitles: nil)
使用:
button2Alert.show()
因为现在它崩溃,我似乎不能得到正确的语法。
As of right now it is crashing and I just can't seem to get the syntax right.
推荐答案
从 UIAlertView
类:
// UIAlertView已弃用。使用 UIAlertController 替换UIAlertControllerStyleAlert的
preferredStyle
// UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead
在iOS 8上, :
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
现在 UIAlertController
是一个单独的类,用于创建和交互我们所知道的 UIAlertView
s和 UIActionSheet
在iOS 8上。
Now UIAlertController
is a single class for creating and interacting with what we knew as UIAlertView
s and UIActionSheet
s on iOS 8.
修改:处理操作:
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in
switch action.style{
case .Default:
print("default")
case .Cancel:
print("cancel")
case .Destructive:
print("destructive")
}
}))
为Swift 3编辑:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
这篇关于如何在Swift中创建UIAlertView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!