UIAlertView在Swift中不起作用

App*_*Kid 3 uialertview ios swift

当我在swift中使用此代码时,我不知道为什么应用程序通过在"alertView.show()"部分中显示断点来终止,有人请帮助我.

var alertView = UIAlertView(
    title: "Hey",
    message: "Hello",
    delegate: self,
    cancelButtonTitle: "Cancel"
)
alertView.show()
Run Code Online (Sandbox Code Playgroud)

Guy*_*lon 17

从Xcode 6.0 UIAlertView类:

UIAlertView已弃用.使用UIAlertController而不是UIAlertControllerStyleAlert的preferredStyle.

在swift(iOS 8和OS X 10.10)上,您可以这样做:

var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel))
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in
                println("User click Ok button")
            }))
        self.presentViewController(alert, animated: true, completion: nil)

func handleCancel(alertView: UIAlertAction!)
        {
            println("User click cancel button")
        }
Run Code Online (Sandbox Code Playgroud)

如果你想在'ActionSheet'中使用'Alert',你只需要更改UIAlertControllerStyle,例如:

var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.ActionSheet)
Run Code Online (Sandbox Code Playgroud)