在swift中显示UIAlertView时出错

问题描述 投票:9回答:7

我试图在我的快速应用程序中显示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
    }
}

怎么做对了? :)

uialertview swift uialertcontroller
7个回答
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)

15
投票

即使UIAlertView在iOS 8中被弃用,你也可以使用它而不是通过它的init函数。例如:

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

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


1
投票

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

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

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")
    }
}))

0
投票

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

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

代码“alert.addButtonWithTitle(”Understood“)”为用户在读取错误消息后添加了一个按钮。

希望这对你有所帮助。


0
投票

Xcode 10, Swift 4.2 version

        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)

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)
© www.soinside.com 2019 - 2024. All rights reserved.