【问题标题】:UIAlertView is Not Working in SwiftUIAlertView 在 Swift 中不起作用
【发布时间】:2014-07-27 21:11:05
【问题描述】:

当我快速运行这段代码时,我不知道为什么应用程序会在“alertView.show()”部分显示一个断点而终止,请有人帮助我。

var alertView = UIAlertView(
    title: "Hey",
    message: "Hello",
    delegate: self,
    cancelButtonTitle: "Cancel"
)
alertView.show()

【问题讨论】:

    标签: ios uialertview swift


    【解决方案1】:

    使用以下方式:

    var altMessage = UIAlertController(title: "Warning", message: "This is Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
    altMessage.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(altMessage, animated: true, completion: nil)
    

    【讨论】:

    【解决方案2】:

    来自 Xcode 6.0 UIAlertView 类:

    UIAlertView 已弃用。将 UIAlertController 与首选样式一起使用 UIAlertControllerStyleAlert 代替。

    在 swift(iOS 8 和 OS X 10.10)上,您可以这样做:

    var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel))
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in
                    println("User click Ok button")
                }))
            self.presentViewController(alert, animated: true, completion: nil)
    
    func handleCancel(alertView: UIAlertAction!)
            {
                println("User click cancel button")
            }
    

    如果您想在“ActionSheet”中使用“Alert”,您只需更改 UIAlertControllerStyle,例如:

    var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.ActionSheet)
    

    【讨论】:

    • 你能告诉我如何在其中添加一个 TextField 吗??
    • @AppleKid,添加 alert.addTextFieldWithConfigurationHandler(configurationTextField),以及函数:func configurationTextField(textField: UITextField!) { println("configurat租用 TextField") if let tField = textField { self.textField = 文本字段! self.textField.text = "Hello world" } } 现在您可以在 handleCancel 或 handleOk 闭包上打印用户输入: println(self.textField.text)
    • 试图在闭包中使用 self.textField.text 但它说Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit
    【解决方案3】:

    UIAlertView 在 iOS 8 中已被弃用,但 Swift 支持 iOS7,你不能在 iOS 7 上使用 UIAlertController。 添加以下方法解决问题:

    func showAlert(title:NSString, message:NSString,owner:UIViewController) {
        if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {
            var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            owner.presentViewController(alert, animated: true, completion: nil)
        }
        else {
            let alertView = UIAlertView(title: title, message: message, delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OK")
            alertView.alertViewStyle = .Default
            alertView.show()
        }
    }
    

    并在代码中的任何位置调用该方法,如下所示:

    showAlert(APP_NAME,message: "Add your alert message here" ,owner: self)
    

    【讨论】:

      【解决方案4】:

      对我来说最好的方法是......

      class ViewController: UIViewController, UIAlertViewDelegate {
      var allarme = UIAlertView(title: "Warning", message: "This is a best way to create a alarm message", delegate: self, cancelButtonTitle: "OK")
              allarme.show()
      

      记得在 UIAlertViewDelegate 类上导入

      【讨论】:

        猜你喜欢
        • 2015-10-30
        • 1970-01-01
        • 1970-01-01
        • 2013-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-22
        相关资源
        最近更新 更多