Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UIAlertView错误_Swift_Uialertview - Fatal编程技术网

Swift UIAlertView错误

Swift UIAlertView错误,swift,uialertview,Swift,Uialertview,我正在使用Swift制作一个股票应用程序,我正在尝试在投资组合中发出警报,我发现了一些错误。我已经查找了最新版本的Alertview,但是没有任何效果,所以我只是想寻求一些帮助 static func showAlert(title: String, message: String, caller: UITableViewController) { if #available(iOS 8.0, *) { let alertController = UIAlertContr

我正在使用Swift制作一个股票应用程序,我正在尝试在投资组合中发出警报,我发现了一些错误。我已经查找了最新版本的Alertview,但是没有任何效果,所以我只是想寻求一些帮助

static func showAlert(title: String, message: String, caller: UITableViewController) {
    if #available(iOS 8.0, *) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    } else {
        // Fallback on earlier versions
    }

    if #available(iOS 8.0, *) {
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
            UIAlertAction in
        }
    } else {
        // Fallback on earlier versions
    }

    if #available(iOS 8.0, *) {
        alertcontroller.addAction(okAction)
    } else {
        // Fallback on earlier versions
    }


    caller.presentViewController(alertController, animated: true, completion: nil)
}
}

我的错误我正在使用未解析的标识符alertcontroller。

看起来您正在代码块中实例化
alertcontroller
,您在代码块中指定只有在iOS 8.0可用时才能执行它。请尝试以下方法:

var alertController:UIAlertController?

if #available(iOS 8.0, *) {
    alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
} else {
    // Fallback on earlier versions
    // Do whatever else with the alertController
}
根据您所看到的错误,问题是您试图在名为
alertController
的方法末尾显示一个
UIAlertController
,但它告诉您没有任何名为
alertController
的属性,它告诉我您实例化它的代码行没有被调用,这意味着如果条件不满足

我会这样做,它是更新的,更简单:

func showAlert(title: String, message: String, caller: UITableViewController) {

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
    alertController.addAction(okAction)

    caller.present(alertController, animated: true, completion: nil)
}

您的代码被全部拆分,并且您的
alertController
声明的范围仅限于第一个
if
语句

最好的方法是将所有代码正确地组合在一起:

Swift 2:

static func showAlert(title: String, message: String, caller: UITableViewController) {
    if #available(iOS 8.0, *) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { action in
        // do something
        }
        alertcontroller.addAction(okAction)
        caller.presentViewController(alertController, animated: true, completion: nil)
    } else {
        // Fallback to UIAlertView on earlier versions
    }
}
当然,如果您不需要支持iOS 7,那么就不需要这些。只要做:

static func showAlert(title: String, message: String, caller: UITableViewController) {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { action in
        // do something
    }
    alertcontroller.addAction(okAction)
    caller.presentViewController(alertController, animated: true, completion: nil)

}
Swift 3:

static func showAlert(title: String, message: String, caller: UITableViewController) {
    if #available(iOS 8.0, *) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default) { action in
            // do something
        }
        alertController.addAction(okAction)
        caller.present(alertController, animated: true, completion: nil)
    } else {
        // Fallback to UIAlertView on earlier versions
    }
}

你的错误是什么?@Pierce我更新了我的问题。你需要支持iOS 7吗?如果没有,就没有必要进行所有的版本检查。我会编写一些代码,这样会更好地工作,但让我问你,为什么要检查iOS版本?你正在使用低于8.0的设备吗?不,我只是使用了一个旧项目的代码,我需要更新它@Pierce我正在使用最新的IOS。请查看我的更新答案。我认为你试图做的是完全没有必要的。它说UIAlertControllerStyle没有成员“alert”。下一行显示的是预期的分隔符。但Swift 3仍然没有更新。Swift 3语法将显示
UIAlertActionStyle.default
caller.present(alertController,动画:true,完成:nil)
@Pierce操作发布的Swift 2代码。答案是斯威夫特2。这个问题没有提到使用Swift 3。如果你认为我在批评,我道歉。我只是想提一下,以防有人无意中发现了这一点,需要使用Swift 3