11

我试图在我的 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
    }
}

怎么做才对?:)

4

7 回答 7

26

Swift 5

You should do it this way:

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)
于 2014-06-04T14:51:05.597 回答
15

即使UIAlertView在 iOS8 中已贬值,您也可以使用它,但不能通过它的 init 函数。例如:

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

至少这是迄今为止我能够成功使用UIAlertView. 不过,我不确定这有多安全。

于 2014-06-04T14:55:19.377 回答
1

这就是我用来快速弹出警报的方法。

let myAlert = UIAlertView(title: "Invalid Login",     
message: "Please enter valid user name",       
delegate: nil, cancelButtonTitle: "Try Again") 
myAlert.show()
于 2014-08-04T16:08:12.620 回答
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")
    }
}))
于 2014-11-10T10:07:49.167 回答
0

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

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

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

希望这对您有所帮助。

于 2015-03-05T14:49:01.477 回答
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)
于 2019-05-18T09:17:23.990 回答
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)
于 2019-03-25T10:44:39.183 回答