无法将 LaunchViewController 转换为 UINavigationController

can't cast LaunchViewController to UINavigationController

我正在尝试从 AppDelegate.swift 检查网络状态,而不是从 LaunchViewController 检查。

我在 AppDelegate 中写了 showOfflinePage 当我关闭网络时出现此错误

Could not cast value of type 'reachability_playground.LaunchViewController' (0x1095b5f20) to 'UINavigationController' (0x114bb2a20). 2019-01-06 16:42:04.079430-0500 reachability-playground[2781:93635] Could not cast value of type 'reachability_playground.LaunchViewController' (0x1095b5f20) to 'UINavigationController' (0x114bb2a20).

   private func showOfflinePage() -> Void {
        DispatchQueue.main.async {
            let storyboard = UIStoryboard(name: "Main", bundle: nil);
            let viewController: LaunchViewController = storyboard.instantiateViewController(withIdentifier: "LaunchViewController") as! LaunchViewController;

            // Then push that view controller onto the navigation stack
            let rootViewController = self.window!.rootViewController as! UINavigationController
            rootViewController.pushViewController(viewController, animated: true);
        }
    }

根据异常消息,您的根视图控制器是 LaunchViewController 的实例,而不是 UINavigationController 的实例,因此强制向下转换失败。

您需要检查故事板并确保入口点场景是导航控制器。