Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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 如何在调用函数的控制器中显示全局函数内部的警报?_Swift_Uiviewcontroller_Alert - Fatal编程技术网

Swift 如何在调用函数的控制器中显示全局函数内部的警报?

Swift 如何在调用函数的控制器中显示全局函数内部的警报?,swift,uiviewcontroller,alert,Swift,Uiviewcontroller,Alert,我有一个功能,将同步iCloud,我将从几个视图控制器调用它。如果iCloud不可用,我想让它显示一个警报(要么它总是关闭,要么用户自上次访问后注销,不管怎样)。是否有一种方法可以在函数中添加警报代码,并将其显示在调用函数的VC中 func synciCloud(){ if iCloudIsAvailable() { // stuff to sync iCloud } else { // where I'd like to call the al

我有一个功能,将同步iCloud,我将从几个视图控制器调用它。如果iCloud不可用,我想让它显示一个警报(要么它总是关闭,要么用户自上次访问后注销,不管怎样)。是否有一种方法可以在函数中添加警报代码,并将其显示在调用函数的VC中

func synciCloud(){

    if iCloudIsAvailable() {
        // stuff to sync iCloud
    } else {
        // where I'd like to call the alert
    }

}
随后:

class List: UIViewController {
    override func viewDidLoad() {
        synciCloud()
        // this won't actually run on viewDidLoad but I'm just showing an example.
    }
}

那么,有没有办法做到这一点,或者我的想法完全错了?谢谢。

您已经尝试向alertViewController演示了吗
UIApplication.shared.keyWindow?.rootViewController?.present(YourAlertViewController,动画:true,完成:nil)
此外,此答案可能会帮助您:@ReyBruno如果根视图控制器已经有一个显示的控制器,则您的第一条评论不起作用。