UIAlertView 使 iOS 7 上的应用程序崩溃 [英] UIAlertView is crashing app on iOS 7

查看:29
本文介绍了UIAlertView 使 iOS 7 上的应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在 App Store 上发布了我的应用,但是我刚刚收到一封电子邮件,告诉我存在崩溃问题.

I have just released my app on the App Store however I have just been emailed and told that there is issues with crashing.

问题在于警报视图在它们应该出现时使我的应用程序崩溃(仅在 iOS 7 中).我有一些应该解决这个问题的代码,但它似乎在某些版本的 iOS 7 中不起作用.

The issue is with Alert Views which crash my app when they are supposed to appear (only in iOS 7). I had some code in place that was supposed to fix this issue, however it doesn't seem to work in certain versions of 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 上崩溃?

What can I do to ensure that my app doesn't crash on any version of iOS 7 or 8?

推荐答案

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

I had the same problem in the relese build. It seems an internal bug of the swift compiler (using Xcode 6.0.1 (6A317) )

我用这个方法用 objC helper 类实际解决了:

I solved actually with a objC helper class with this method:

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

在我的 swift 代码中调用桥头

called in my swift code with a bridge header

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

这篇关于UIAlertView 使 iOS 7 上的应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆