为什么即使将导航控制器嵌入故事板并使用推播功能,我的导航控制器仍为零?

时间:2019-04-01 06:55:37

标签: ios swift uinavigationcontroller

我试图读取Stackoverflow上的多个线程,但是我认为我已经实现了所有这些线程,但仍然无法正常工作。这是我的故事板

enter image description here

所以我像下面的图片一样隐藏(取消选中)导航栏的可见性,因为我想像上面的图片(右图)那样实现自己的“导航标题”: enter image description here

当按下后退按钮时,我使用以下代码:

self.navigationController?.popViewController(animated: true)

但不幸的是,检查后,导航控制器为零,我无法返回到以前的VC。

我在这样的应用程序委托中设置了一些代码,以设置导航。如果用户已经登录,则将其导航到HomeVC(主选项卡栏),否则将定向到类似于我上面的故事板的登录顺序

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


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

        // to print Local Database Location, uncomment the line below if you want to trace the location of Realm Database / User Default
        // print("Location of Realm Database: \(Realm.Configuration.defaultConfiguration.fileURL)")



        checkHasLoggedInOrNot()




        return true
    }




}

extension AppDelegate {
    // MARK: - Helper Methods

    func checkHasLoggedInOrNot() {
        let userHasLoggedIn = AuthService.shared.hasLoggedIn

        if userHasLoggedIn {
            goToMainTabBar()
        } else {
            goToAuthVC()
        }
    }


}

extension AppDelegate {
    // MARK: - Navigation

    func goToMainTabBar() {
        let storyboard = UIStoryboard(name: StoryBoardName.Main.rawValue, bundle: nil)
        let mainTabBar = storyboard.instantiateViewController(withIdentifier: MainStoryboardData.StoryBoardID.MainTabBar.rawValue)
        window?.rootViewController = mainTabBar
    }

    func goToAuthVC() {
        let storyboard = UIStoryboard(name: StoryBoardName.Auth.rawValue, bundle: nil)
        let authVC = storyboard.instantiateViewController(withIdentifier: AuthStoryboardData.StoryBoardID.AuthVC.rawValue)
        window?.rootViewController = authVC
    }
}

也许是下面的代码?

func goToAuthVC() {
        let storyboard = UIStoryboard(name: StoryBoardName.Auth.rawValue, bundle: nil)
        let authVC = storyboard.instantiateViewController(withIdentifier: AuthStoryboardData.StoryBoardID.AuthVC.rawValue)
        window?.rootViewController = authVC
    }

因为它指向AuthVC?不导航控制器?

这里出了什么问题?

1 个答案:

答案 0 :(得分:2)

Push/Pop只有在Navigation stack中有window时才可能。

将您的goToAuthVC替换为以下内容-

func goToAuthVC() {

    let storyboard = UIStoryboard(name: StoryBoardName.Auth.rawValue, bundle: nil)
    let authVC = storyboard.instantiateViewController(withIdentifier: AuthStoryboardData.StoryBoardID.AuthVC.rawValue)
    let navigationController  = UINavigationController(rootViewController: authVC)
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()

}