在swift中显示UIAlertView时出错

时间:2014-06-04 14:41:23

标签: uialertview swift uialertcontroller

我试图在我的swift App中显示UIAlertView

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

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

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

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

=> 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
    }
}

怎么做对了? :)

7 个答案:

答案 0 :(得分:20)

你应该这样做:

let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Button", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)

答案 1 :(得分:15)

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

    var alert = UIAlertView()
    alert.title = "Title"
    alert.message = "message"
    alert.show()

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

答案 2 :(得分:1)

这是我用来在swift中制作警报弹出窗口的方法。

let myAlert = UIAlertView(title: "Invalid Login",     
message: "Please enter valid user name",       
delegate: nil, cancelButtonTitle: "Try Again") 
myAlert.show()

答案 3 :(得分:0)

var 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)

处理行动:

alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { 
    action in switch action.style {
    case .Default:
        println("default")
    case .Cancel:
        println("cancel")
    case .Destructive:
        println("destructive")
    }
}))

答案 4 :(得分:0)

我成功地使用此代码显示了UIAlertView:

var alert = UIAlertView()
alert.title = "Title"
alert.message = "Message"
alert.addButtonWithTitle("Understood")
alert.show()

代码“alert.addButtonWithTitle(”Understood“)” 添加一个按钮,供用户在阅读错误消息后进行推送。

希望这会对你有所帮助。

答案 5 :(得分:0)

Xcode 10,Swift 4.2版本

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

答案 6 :(得分:0)

let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertController.Style.alert)
                    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil))
                    self.present(alert, animated: true, completion: nil)