swift - 如何快速更改 rootViewController?
问题描述
我正在开发一个没有登录注销功能的应用程序。当用户打开应用程序时,他会看到主屏幕。现在在设置页面中有一个名为 Pin Lock 应用程序的选项。当用户选择此选项时,会出现 PinScreen(如 OTP 屏幕),然后用户设置 Pin。之后,当用户关闭应用程序时,首先会出现 Pin 锁定屏幕,要求用户输入 pin。那么我该怎么做呢?
if UserDefaults.standard.bool(forKey: "LoggedIn") == false {
let story = UIStoryboard(name: "Main", bundle:nil)
let vc = story.instantiateViewController(withIdentifier: "EnterPin") as! UINavigationController
UIApplication.shared.windows.first?.rootViewController = vc
UIApplication.shared.windows.first?.makeKeyAndVisible()
let enterPinViewController = self.storyboard.instantiateViewController(identifier: "EnterPinViewController") as! EnterPinViewController
self.navigationController.present(enterPinViewController, animated: true, completion: nil)
} else if UserDefaults.standard.bool(forKey: "LoggedIn") == true {
let story = UIStoryboard(name: "Main", bundle:nil)
let vc = story.instantiateViewController(withIdentifier: "MainController") as! UINavigationController
UIApplication.shared.windows.first?.rootViewController = vc
UIApplication.shared.windows.first?.makeKeyAndVisible()
let mainViewController = self.storyboard.instantiateViewController(identifier: "ViewController") as! ViewController
self.navigationController.present(mainViewController, animated: true, completion: nil)
}
目前我将 UserDefaults 值设置为 true 和 false,并将 Pin 保存在 UserDefaults 本身中。我曾尝试更改 rootViewController 但没有发生任何事情,这意味着每次主屏幕出现而不是 Pin Screen 时。
解决方案
let viewController = MainViewController()
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
guard let window = appDelegate.window else {
return
}
window.rootViewController = viewController
推荐阅读
- java - Java编译器优化可以消除volatile
- json - 如何自动将 Eventbrite 事件推送到 Facebook?
- python - Python:Json 响应解析
- python - 存储 numpy 数组的最简单方法
- android - 如何从 webview 调用对话框?
- javascript - 如何在 React JS 中使用脚本
- pycharm - PyCharm 键盘快捷键和突出显示问题
- c# - C#动态控制anchor失形
- plugins - 构建 NativeScript 插件时如何访问硬件层?
- python - Pandas - Dataframe 有包含列表的列。如何按列表中的元素分组?