我一直在开发一个 Swift 项目,我有两个视图控制器,登录视图控制器和主页视图控制器。当用户启动应用程序时,如果用户未登录,我想显示登录视图控制器,另一方面,如果用户登录,我想显示主页视图控制器。
所以流程会是这样的。
当用户未登录时,显示
- 登录视图控制器
- 主页视图控制器
当用户已经登录时,显示
- 主页视图控制器
在场景代表中,我写了
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: scene.coordinateSpace.bounds)
window?.windowScene = scene
window?.rootViewController = HomeViewController() or LoginViewController() depending on the user's login status
window?.makeKeyAndVisible()
}
我想知道是否应该将 HomeViewController 应用为 rootviewcontroller,无论用户的登录状态如何(并且当用户未登录时可能在 homeVC 上显示 loginVC),或者我应该根据用户的登录状态切换视图控制器。
那么,在这种情况下,切换rootviewcontroller还有什么意义呢?以及为什么切换根视图控制器是(或不重要)?
当我将视图控制器应用于根视图控制器属性时,有什么我应该考虑的吗?
// SceneDelegate.swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
// if user is logged in before
if let loggedUsername = UserDefaults.standard.string(forKey: "username") {
window?.rootViewController = HomeViewController()
} else {
// if user isn't logged in
window?.rootViewController = LoginViewController()
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)