繁体   English   中英

Swift Alert弹出窗口出现在模拟器iphone4s(8.1)中,而不是iphone4s(7.1)中

[英]Swift Alert popup is appearing in simulator iphone4s(8.1), not iphone4s (7.1)

我编写了代码,以在单击按钮时显示一个简单的警报弹出窗口。 在模拟器iPhone 4s(8.1)中尝试该应用程序时,它可以按预期运行,但是在模拟器iPhone 4s(7.1)中尝试时,该应用程序始终崩溃。

这里的代码:

@IBAction func buttonPressed(sender:UIButton){

    let controller = UIAlertController(title: "This is a title", message: "Hello, my friend", preferredStyle: UIAlertControllerStyle.Alert)
    let cancelAction = UIAlertAction(title: "Phew!", style: .Cancel, handler: nil)
    controller.addAction(cancelAction)
    self.presentViewController(controller, animated: true, completion: nil)
}

这是错误消息:

第一行代码(创建“ controller”常量的代码)以绿色突出显示,并显示消息“线程1:EXC_BAD_ACCESS(Code = 1,address = 0x10)”。

我将不胜感激任何帮助

UIAlertController适用于iOS> = 8.0

您必须为iOS <8.0使用UIAlertView

不幸的是,UIAlertController仅在iOS 8.0和更高版本中可用,这里是文档,并且在右侧进行了说明: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/#//apple_ref/ DOC / UID / TP40014538-CH1-SW2

我相信这取代了现在不推荐使用的UIAlertView,可以在Apple文档中找到: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html#//apple_ref/occ/cl / UIAlertView中

Swift不喜欢旧的,而是考虑和“ if”子句,并使用该应用为相应的iOS系统创建UIAlertView和UIAlertController。

感谢Dom Bryan推荐的链接,我管理了以下解决方案:

@IBAction func buttonPressed(sender:UIButton){

    if NSClassFromString("UIAlertController") == nil{
        let alert = UIAlertView(title: "This is a title", message: "I am an iOS7 alert", delegate: self, cancelButtonTitle: "Phew!")
        alert.show()
    }else{
        let controller = UIAlertController(title: "This is a title", message: "Hello, my friend", preferredStyle: .Alert)
        let cancelAction = UIAlertAction(title: "Phew!", style: .Cancel, handler: nil)
        controller.addAction(cancelAction)
        self.presentViewController(controller, animated: true, completion: nil)
    }
}

而且在iOS7和iOS8设备上运行正常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM