UIAlertView正在iOS 7上崩溃应用程序

时间:2014-09-18 19:58:20

标签: swift uialertview

我刚刚在App Store上发布了我的应用程序,但我刚收到电子邮件,并告知崩溃存在问题。

问题在于警报视图会在应用程序出现时崩溃(仅在iOS 7中)。我有一些应该修复此问题的代码,但它似乎不适用于某些版本的iOS 7。

这是我目前的代码:

@IBAction func resetAllButton(sender : AnyObject) {
    //If statement to check whether the modern style alert is available. Prior to iOS 8 it is not.
    if let gotModernAlert: AnyClass = NSClassFromString("UIAlertController") {

        println("UIAlertController can be instantiated")

        var alert = UIAlertController(title: "Start Over", message: "Are you sure you want to start over? This will erase your budget and all transactions.", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "I'm sure!", style: UIAlertActionStyle.Default, handler:{ (ACTION :UIAlertAction!)in
            self.resetView()
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

        self.presentViewController(alert, animated: true, completion: nil)
    }
    else {

        println("UIAlertController can NOT be instantiated")

        var alertView = UIAlertView()
        alertView.delegate = self
        alertView.title = "Start Over"
        alertView.message = "Are you sure you want to start over? This will erase your budget and all transactions."
        alertView.addButtonWithTitle("I'm sure!")
        alertView.addButtonWithTitle("Cancel")
        alertView.show()
    }
}

我可以做些什么来确保我的应用在任何版本的iOS 7或8上都不会崩溃?

2 个答案:

答案 0 :(得分:3)

我在修复版本中遇到了同样的问题。 这似乎是swift编译器的内部错误(使用Xcode 6.0.1(6A317))

我用这个方法实际上解决了一个objC助手类:

+ (BOOL) checkIfClassExists:(NSString *)className {
    id c = objc_getClass([className cStringUsingEncoding:NSASCIIStringEncoding]);
    if (c != nil) {
        return YES;
    } else {
        return NO;
    }
}

在我的swift代码中调用了一个桥头

if ObjCFix.checkIfClassExists("UIAlertController") {
    //iOS 8 code here
} else {
    //iOS 7 code here
}

答案 1 :(得分:0)

这是我的拖放快速解决方案:

//Alerts change in iOS8, this method is to cover iOS7 devices
func CozAlert(title: String, message: String, action: String, sender: UIViewController){

    if respondsToSelector("UIAlertController"){
        var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: action, style: UIAlertActionStyle.Default, handler:nil))
        sender.presentViewController(alert, animated: true, completion: nil)
    }
    else {
        var alert = UIAlertView(title: title, message: message, delegate: sender, cancelButtonTitle:action)
        alert.show()
    }
}

这样打电话:

CozAlert("reportTitle", message: "reportText", action: "reportButton", sender: self)

请注意,这仅适用于最基本的警报,您可能需要其他高级内容代码。