我试图读取Stackoverflow上的多个线程,但是我认为我已经实现了所有这些线程,但仍然无法正常工作。这是我的故事板
所以我像下面的图片一样隐藏(取消选中)导航栏的可见性,因为我想像上面的图片(右图)那样实现自己的“导航标题”:
当按下后退按钮时,我使用以下代码:
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?不导航控制器?
这里出了什么问题?
答案 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()
}