在Swift中显示警报视图会导致EXC BAD ACCESS

Ale*_*yne 3 ios swift

我有以下视图控制器.它只是从文本字段中读取要在a中显示的值UIAlertView.

import UIKit

class ViewController: UIViewController {

    @IBOutlet var textField: UITextField!

    @IBAction func pressButton(sender: UIButton) {
        let name = textField.text

        let alert = UIAlertView(
            title: "Hello!",
            message: "How are you today, \(name). I'm lovely!",
            delegate: nil,
            cancelButtonTitle: "Thanks!"
        )

        alert.show() // EXC_BAD_ACCESS
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么会alert.show()崩溃EXC_BAD_ACCESS?这是我的实例发生了什么UIAlertView?为什么它alert不像我认为它应该是?

Nir*_*iya 8

尝试使用var而不是lat如下

var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)