简体   繁体   English

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

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

I have written code to display a simple alert popup when a button is clicked. 我编写了代码,以在单击按钮时显示一个简单的警报弹出窗口。 When trying the app in simulator iPhone 4s (8.1), it's working as expected, but when trying in simulator iPhone 4s (7.1), the app keeps crashing. 在模拟器iPhone 4s(8.1)中尝试该应用程序时,它可以按预期运行,但是在模拟器iPhone 4s(7.1)中尝试时,该应用程序始终崩溃。

Here the code: 这里的代码:

@IBAction func buttonPressed(sender: UIButton) { @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)
}

Here's the error message: 这是错误消息:

The first line code (the one creating the "controller" constant) is highlighted in green with the message "Thread 1: EXC_BAD_ACCESS(Code=1,address=0x10)" 第一行代码(创建“ controller”常量的代码)以绿色突出显示,并显示消息“线程1:EXC_BAD_ACCESS(Code = 1,address = 0x10)”。

I would appreciate any help 我将不胜感激任何帮助

UIAlertController is available for iOS >= 8.0 UIAlertController适用于iOS> = 8.0

You have to use UIAlertView for iOS < 8.0 您必须为iOS <8.0使用UIAlertView

UIAlertController is only available in iOS 8.0 and on wards unfortunately, here is documentation and it states this on the right: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/#//apple_ref/doc/uid/TP40014538-CH1-SW2 不幸的是,UIAlertController仅在iOS 8.0和更高版本中可用,这里是文档,并且在右侧进行了说明: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/#//apple_ref/ DOC / UID / TP40014538-CH1-SW2

I believe this replaced the now deprecated UIAlertView which can be found here in Apple Documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html#//apple_ref/occ/cl/UIAlertView 我相信这取代了现在不推荐使用的UIAlertView,可以在Apple文档中找到: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html#//apple_ref/occ/cl / UIAlertView中

Swift doesn't like the old but consider and "if" clause and create both a UIAlertView and UIAlertController for the respective iOS system using the app. Swift不喜欢旧的,而是考虑和“ if”子句,并使用该应用为相应的iOS系统创建UIAlertView和UIAlertController。

Thanks to recommended links from Dom Bryan, I managed the following solution: 感谢Dom Bryan推荐的链接,我管理了以下解决方案:

@IBAction func buttonPressed(sender: UIButton) { @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)
    }
}

And it's working fine on iOS7 and iOS8 devices 而且在iOS7和iOS8设备上运行正常

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

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