简体   繁体   中英

Blank screen first time launching app xcode

I am having a little issue with logging into an app the first time with a blank screen and I am getting the warning "Attempt to present on whose view is not in the window hierarchy!" After I close and relaunch, the views appear fine. I believe it has something to do with the rootViewController but not sure... Thanks in advance for any help or direction!

App delegate

    @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
        }

Navigation controller as 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
            })
        }
    }

// log in function called

    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)
    }

好的,所以我的最后一个答案是错误的(我将其删除了),原因是在应用程序中有一个键盘窗口,它是您的navcontroller,并且在此窗口上无法显示任何内容,直到它将加载其子视图(即使它没有加载) t any),而这将发生在viewdidappear上,因此您应该将代码从viewdidload放到那里。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM