【发布时间】:2021-04-24 05:07:33
【问题描述】:
当用户登录我的应用程序时,将为键“isLogged”保存一个 Swift UserDefault 并设置为 true。如果此 UserDefault 的值为 true,我的目标是让应用程序委托显示不同的视图控制器。
我已经为此苦苦挣扎了好几个星期,因此想看看是否有人知道任何问题。我在 AppDelegate.swift 中调用一个函数来实例化视图控制器,但由于某种原因,视图控制器从未显示。执行了打印语句,所以我知道设置了值,但是为什么没有显示视图控制器?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
checkDefault()
return true
}
func checkDefault() {
if (UserDefaults.standard.bool(forKey: "isLogged") == true) {
print("LoggedIn is true")
let vc = UIStoryboard(name: "Main", bundle:
Bundle.main).instantiateViewController(withIdentifier:
"parent_vc") as! ParentHomeViewController
let navVC = UINavigationController(rootViewController: vc)
let share = UIApplication.shared.delegate as? AppDelegate
share?.window?.rootViewController = navVC
share?.window?.makeKeyAndVisible()
}
}
谢谢!
【问题讨论】:
-
为什么不改变视图控制器层次结构?让 ApDelegate 实例化一个 single VC - 就像它通常做的那样 - 并让 那个 根 VC 实例化它需要的东西?
-
我该怎么做?
-
请分享您在调试器窗口中得到的信息。
标签: ios swift swift5 appdelegate userdefaults