我将如何在 Swift 中创建 UIAlertView? [英] How would I create a UIAlertView in Swift?
问题描述
我一直在努力在 Swift 中创建一个 UIAlertView,但由于某种原因我无法得到正确的语句,因为我收到此错误:
<块引用>找不到接受提供的init"的重载论据
我是这样写的:
let button2Alert: UIAlertView = UIAlertView(title: "Title", message: "message",委托:self,cancelButtonTitle:OK",otherButtonTitles:nil)
然后称之为我正在使用的:
button2Alert.show()
截至目前,它正在崩溃,我似乎无法正确使用语法.
来自 UIAlertView
类:
//UIAlertView 已弃用.将 UIAlertController 与优选样式为 UIAlertControllerStyleAlert 代替
在 iOS 8 上,您可以这样做:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))self.presentViewController(警报,动画:true,完成:nil)
现在 UIAlertController
是一个单一的类,用于在 iOS 8 上创建我们所知的 UIAlertView
s 和 UIActionSheet
s 并与之交互.>
处理动作:
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in切换动作样式{案例.默认:打印(默认")案例.取消:打印(取消")案例.破坏性:打印(破坏性")}}}))
针对 Swift 3 进行
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))self.present(警报,动画:true,完成:nil)
针对 Swift 4.x 进行
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: .alert)alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in切换动作样式{案例.默认:打印(默认")案例.取消:打印(取消")案例.破坏性:打印(破坏性")}}))self.present(警报,动画:true,完成:nil)
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:
Could not find an overload for 'init' that accepts the supplied arguments
Here is how I have it written:
let button2Alert: UIAlertView = UIAlertView(title: "Title", message: "message",
delegate: self, cancelButtonTitle: "OK", otherButtonTitles: nil)
Then to call it I'm using:
button2Alert.show()
As of right now it is crashing and I just can't seem to get the syntax right.
From the UIAlertView
class:
// UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead
On iOS 8, you can do this:
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)
Now UIAlertController
is a single class for creating and interacting with what we knew as UIAlertView
s and UIActionSheet
s on iOS 8.
Edit: To handle actions:
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")
}
}}))
Edit for 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)
Edit for Swift 4.x:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: .alert)
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")
}
}))
self.present(alert, animated: true, completion: nil)
这篇关于我将如何在 Swift 中创建 UIAlertView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!