1

我在第一次使用空白屏幕登录应用程序时遇到了一点问题,我收到警告“尝试在其视图不在窗口层次结构中呈现!” 关闭并重新启动后,视图看起来很好。我相信这与rootViewController但不确定...在此先感谢您的帮助或指导!

应用委托

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

        var window: UIWindow?
        var ref:FIRDatabaseReference?
        var databaseHandle:FIRDatabaseHandle?

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

            window = UIWindow(frame: UIScreen.main.bounds)
            window?.makeKeyAndVisible()
            window?.rootViewController = MainNavigationController()

            FIRApp.configure()
            ref = FIRDatabase.database().reference()

            return true
        }

导航控制器作为 rootViewController

    class MainNavigationController: UINavigationController {

        var segmentedController: UISegmentedControl!

        override func viewDidLoad() {
            super.viewDidLoad()

            let vc1 = TravelersFeedVC()
            let vc2 = ProfileVC()

            if isLoggedIn() {
                // assume user is logged in
                let homeController = HomeController()
                viewControllers = [homeController]
                homeController.firstViewController = vc1
                homeController.secondViewController = vc2


            } else {
                perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
            }
        }

        fileprivate func isLoggedIn() ->  Bool {
            return UserDefaults.standard.isLoggedIn()
        }

        func showLoginController() {
            let loginController = LoginController()
            present(loginController, animated: true, completion: {
                // perhaps do something here later
            })
        }
    }

// 登录函数调用

    func finishLoggingIn() {
        let rootViewController = UIApplication.shared.keyWindow?.rootViewController
        guard let mainNavigationController = rootViewController as? MainNavigationController else { return }

        let vc1 = TravelersFeedVC()
        let vc2 = ProfileVC()

        if isLoggedIn() {
            // assume user is logged in
            let homeController = HomeController()
            mainNavigationController.viewControllers = [HomeController()]
            homeController.firstViewController = vc1
            homeController.secondViewController = vc2

        } else {
            perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
        }
        UserDefaults.standard.setIsLoggedIn(value: true)
        dismiss(animated: true, completion: nil)
    }
4

1 回答 1

1

好的,所以我的最后一个答案是错误的(我删除了它),事情是在应用程序中有一个 keywindow,它是你的导航控制器,你不能在这个上展示任何东西,直到它加载它的子视图(即使它没有' t 任何),这会发生在 viewdidappear 上,所以你应该把你的代码从 viewdidload 放在那里。

于 2017-04-15T12:42:20.163 回答