我如何在Swift中创建UIAlertView?

本文翻译自:How would I create a UIAlertView in Swift?

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: 我一直在努力在Swift中创建一个UIAlertView,但由于某种原因,我无法正确地说出这句话,因为我收到了这个错误:

Could not find an overload for 'init' that accepts the supplied arguments 找不到接受提供的参数的'init'的重载

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. 截至目前它正在崩溃,我似乎无法使语法正确。


#1楼

参考:https://stackoom.com/question/1cnLL/我如何在Swift中创建UIAlertView


#2楼

From the UIAlertView class: 来自UIAlertView类:

// UIAlertView is deprecated. //不推荐使用UIAlertView。 Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead 使用UIAlertController而不是UIAlertControllerStyleAlert的preferredStyle

On iOS 8, you can do this: 在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)

Now UIAlertController is a single class for creating and interacting with what we knew as UIAlertView s and UIActionSheet s on iOS 8. 现在, UIAlertController是一个单独的类,用于在iOS 8上创建和交互我们所知的UIAlertViewUIActionSheet

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: 编辑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: 编辑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)

#3楼

You can create a UIAlert using the standard constructor, but the 'legacy' one seems to not work: 您可以使用标准构造函数创建UIAlert,但“遗留”似乎不起作用:

let alert = UIAlertView()
alert.title = "Alert"
alert.message = "Here's a message"
alert.addButtonWithTitle("Understood")
alert.show()

#4楼

I found this one, 我找到了这个,

var alertView = UIAlertView();
alertView.addButtonWithTitle("Ok");
alertView.title = "title";
alertView.message = "message";
alertView.show();

not good though, but it works :) 不好,但它的工作:)

Update: 更新:

but I have found on header file as: 但我在头文件中找到了:

extension UIAlertView {
    convenience init(title: String, message: String, delegate: UIAlertViewDelegate?, cancelButtonTitle: String?, otherButtonTitles firstButtonTitle: String, _ moreButtonTitles: String...)
}

somebody may can explain this. 有人可以解释一下。


#5楼

The reason it doesn't work because some value you passed to the function isn't correct. 它不起作用的原因是因为传递给函数的某些值不正确。 swift doesn't like Objective-C, you can put nil to arguments which are class type without any restriction(might be). swift不喜欢Objective-C,你可以将nil设置为没有任何限制的类类型的参数(可能是)。 Argument otherButtonTitles is defined as non-optional which its type do not have (?)at its end. 参数otherButtonTitles定义为非可选的,其类型在其末尾没有(?)。 so you must pass a concrete value to it. 所以你必须传递一个具体的值。


#6楼

Show UIAlertView in swift language :- 用快速语言显示UIAlertView: -

Protocol UIAlertViewDelegate 协议UIAlertViewDelegate

let alert = UIAlertView(title: "alertView", message: "This is alertView", delegate:self, cancelButtonTitle:"Cancel", otherButtonTitles: "Done", "Delete")
alert.show()

Show UIAlertViewController in swift language :- 用快速语言显示UIAlertViewController: -

let alert = UIAlertController(title: "Error", message: "Enter data in Text fields", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值