Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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 试图呈现<;UIAlertController:0x7d92f000>;on<;test.ViewController:0x7c87a990>;其视图不在窗口层次结构中_Swift_Uialertcontroller - Fatal编程技术网

Swift 试图呈现<;UIAlertController:0x7d92f000>;on<;test.ViewController:0x7c87a990>;其视图不在窗口层次结构中

Swift 试图呈现<;UIAlertController:0x7d92f000>;on<;test.ViewController:0x7c87a990>;其视图不在窗口层次结构中,swift,uialertcontroller,Swift,Uialertcontroller,我的ViewController中有一个快速if语句,用于检查internet连接是否可用。如果是,很好,但当互联网未连接时,我会出现以下错误: 尝试显示不在窗口层次结构中的视图 我以前有UIAlertView,但Xcode告诉我它已被弃用,所以我需要将其更改为UIAlertController。执行此操作后,即发生错误 以下是If声明: let alert = UIAlertController(title: "This can't be true", message:"This app ne

我的ViewController中有一个快速if语句,用于检查internet连接是否可用。如果是,很好,但当互联网未连接时,我会出现以下错误:

尝试显示不在窗口层次结构中的视图

我以前有UIAlertView,但Xcode告诉我它已被弃用,所以我需要将其更改为UIAlertController。执行此操作后,即发生错误

以下是If声明:

let alert = UIAlertController(title: "This can't be true", message:"This app needs internet, but you don't have it, please connect", preferredStyle: .Alert)

override func viewDidLoad() {
    super.viewDidLoad()

    // Check for internet Connection
    if Reachability.isConnectedToNetwork() == true {
        print("Internet connection OK")
    } else {
        print("Internet connection FAILED")


        alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
        self.presentViewController(alert, animated: true){}
    }
}

有人能帮上忙吗?

好的,那就用一种快速而肮脏的方式,不确定这是不是正确的方式,堆栈溢出请评论并帮助进一步讨论

理由 您的项目正在创建一个带有透明UIViewController的UIWindow,然后在其上显示UIAlertController。但是,视图尚未完成加载,因此无法找到它

(我认为:S)

解决方案 将其放入viewDidLoad()中:

然后,在viewDidLoad()的范围之外写下以下内容:

对于要加载很多内容的视图,不建议这样做,因为显示警报可能需要一段时间


让我知道这是否有效,并让我知道您的解决方案。

好的,这是一种快速而肮脏的方式,不确定这是否是正确的方式,堆栈溢出请评论并帮助进一步讨论

理由 您的项目正在创建一个带有透明UIViewController的UIWindow,然后在其上显示UIAlertController。但是,视图尚未完成加载,因此无法找到它

(我认为:S)

解决方案 将其放入viewDidLoad()中:

然后,在viewDidLoad()的范围之外写下以下内容:

对于要加载很多内容的视图,不建议这样做,因为显示警报可能需要一段时间


让我知道这是否有效,让我知道您的解决方案。

如果找到internet,您打算怎么做?加载更多内容或转到新的ViewController?@MarkP只需转到新视图-如果已连接internet。好的,给我几分钟时间键入我的答案,这不是一个完美的解决方案,但我以前使用过。如果找到internet,你打算怎么办?加载更多内容或转到新的ViewController?@MarkP只需转到新视图-如果已连接internet。好的,给我几分钟时间键入我的答案,这不是一个完美的解决方案,但我以前使用过。感谢排鱼谢谢排鱼
if Reachability.isConnectedToNetwork() == true {
    print("Internet connection OK")
    // do something
}
override func viewDidAppear(animated: Bool) {
    if Reachability.isConnectedToNetwork() == false {
        print("Internet connection FAILED")
        alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
        self.presentViewController(alert, animated: true){}
    }
}