简体   繁体   English

如何在iOS7兼容设备上使用UIAlertController()作为全局变量?

[英]How to use UIAlertController() as global variable on iOS7 compatible devices?

let alertControllerLoad = UIAlertController()
let alertViewLoad = UIAlertView()

These are both established as global variables in my inbox class. 这些都在我的收件箱类中建立为全局变量。 I check if the device is iOS8 or above before I call UIAlertController but since it creates an instance of the UIAlertController in the class it crashes on ios7 devices. 在调用UIAlertController之前,我检查设备是否为iOS8或更高版本,但是由于它在类中创建UIAlertController的实例,因此在ios7设备上崩溃。 I need them to be global variables since I call them from multiple functions. 我需要它们是全局变量,因为我从多个函数调用它们。 Is there a work around using init() or is this something that doesnt have a work around. 是否有使用init()的解决方法,或者没有解决此问题的方法。

Unfortunately there are no preprocessor calls in Swift, Swift could be conditionally compiled based on the evaluation of build configurations in two functions only as of today. 不幸的是,Swift中没有预处理程序调用,直到今天,Swift仍可以基于对两个功能中的构建配置的评估而有条件地进行编译。

os() for a choice of OSX or iOS and arch() for a choice of x86_64, arm, arm64 or i386. os()用于选择OSX或iOS,而arch()用于选择x86_64,arm,arm64或i386。

So you can't use the Objective-C #if/#endif conditions for checking the iOS version. 因此,您不能使用Objective-C#if /#endif条件来检查iOS版本。

But I think you can make a trick, as you do not need to know the exact iOS version, but only if UIAlertController exists. 但是我认为您可以做个窍门,因为您不需要知道确切的iOS版本,只有在存在UIAlertController And you can check it in the following way: 您可以通过以下方式进行检查:

if (objc_getClass("UIAlertController") == nil) {
   // iOS 7
} else {
   // iOS 8+
}

Then you do not need to have global references to either UIAlertController or UIAlertView but instantiate them locally from inside the if and call. 然后,您不需要具有对UIAlertControllerUIAlertView全局引用,而是可以从if和调用内部本地实例化它们。

For anyone else that might stumble upon this, 对于其他可能会偶然发现的人,

class Alerts { 类别提醒{

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var iOS8Up: Bool = true

lazy var alertControllerLoad = Alerts.set()
let alertViewLoad = UIAlertView()


class func set() -> UIAlertController {

    //get the device model to know what image to post for refresh
    var device : UIDevice = UIDevice.currentDevice();
    var systemVersion = device.systemVersion;
    let numberFormatter = NSNumberFormatter()
    let systemVersionNumber = numberFormatter.numberFromString(systemVersion)
    let systemVersionFloatValue = systemVersionNumber!.floatValue

    if(systemVersionFloatValue < 8.0) {
        return UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.Alert)
    }
    else{
        //returns with actionSheet as preferredStyle
        //will crash on anything less than iOS8
        return UIAlertController()
    }


}

} }

this allows you to make a AlertController a global variable even if you're running iOS7. 这使您即使在运行iOS7时也可以将AlertController设置为全局变量。 I spent far too long on this issue. 我在这个问题上花了太长时间。

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

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