在swift中显示UIAlertView时出错

Dai*_*jan 9 uialertview swift uialertcontroller

我试图在我的快速应用程序中显示UIAlertView

    alert = UIAlertView(title: "",
        message: "bla",
        delegate: self,
        cancelButtonTitle: "OK")
    alert!.show()
Run Code Online (Sandbox Code Playgroud)

=> BAD_ACESS错误: - [_ UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]()

所以我怀疑自己.我把它设置为零

    alert = UIAlertView(title: "",
        message: "bla",
        delegate: nil,
        cancelButtonTitle: "OK")
    alert!.show()
Run Code Online (Sandbox Code Playgroud)

=> ARM_DA_ALIGN错误: - [_ UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]()


完整的代码

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UIAlertViewDelegate {

    @lazy var window = UIWindow(frame: UIScreen.mainScreen().bounds)
    @lazy var locationManager = CLLocationManager()
    var alert: UIAlertView? = nil

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        //setup dummy window
        self.window.backgroundColor = UIColor.whiteColor()
        self.window.rootViewController = UIViewController()
        self.window.makeKeyAndVisible()

        alert = UIAlertView(title: "",
            message: "bla",
            delegate: nil,
            cancelButtonTitle: "OK")
        alert!.show()

    return true
    }
}
Run Code Online (Sandbox Code Playgroud)

怎么做对了?:)

Dan*_*iel 21

你应该这样做:

let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Button", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)


Joh*_*ato 15

即使UIAlertView在iOS8中被折旧,你也可以使用它而不是通过它的init函数.例如:

    var alert = UIAlertView()
    alert.title = "Title"
    alert.message = "message"
    alert.show()
Run Code Online (Sandbox Code Playgroud)

至少这是迄今为止我唯一能够成功使用的方法UIAlertView.我不确定这是多么安全.