我的 swift iOS 应用程序总是在 MainViewController 中启动 - 最初无法在 SceneDeletage 中导航到 LoginViewController
My swift iOS app always launching in MainViewController - can't navigate initially in SceneDeletage to LoginViewController
正如我在标题中所说,我无法导航到登录ViewController。我给你看我的代码:
scenedelegate.swift
[...]
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
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 windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
//self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let rootVC = storyboard.instantiateViewController(identifier: "LoginViewController") as? ViewController else {
print("ViewController not found")
return
}
let rootNC = UINavigationController(rootViewController: rootVC)
self.window?.rootViewController = rootNC
self.window?.makeKeyAndVisible()
}
[...]
在我的登录名中ViewController 我只有文本“FALSE”。
在 MainViewController 我有文本“TRUE”
当我尝试导航到 MainViewController(带箭头)时,它有效,但是当我尝试导航到 LoginViewController 时,我在控制台中输出:ViewController not found
故事板:
StoryboardsID 很好,因为我没有任何例外:
主要ViewController
登录ViewController
如何解决并导航到登录ViewController?
guard
子句 returns nil 因为您正在将具有 class LoginViewController
的 LoginVC 转换为 ViewController
- class 的 mainVC.
将guard let
改成这个
guard let rootVC = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController else {
print("ViewController not found")
return
}
你也能告诉我怎么让它更好看吗?
我只想在里面处理,不是一个外面一个里面。
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 windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var rootVC = storyboard.instantiateViewController(withIdentifier: "MainViewController")
if (false) {
rootVC = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
}
let rootNC = UINavigationController(rootViewController: rootVC)
self.window?.rootViewController = rootNC
self.window?.makeKeyAndVisible()
}
正如我在标题中所说,我无法导航到登录ViewController。我给你看我的代码:
scenedelegate.swift
[...]
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
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 windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
//self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let rootVC = storyboard.instantiateViewController(identifier: "LoginViewController") as? ViewController else {
print("ViewController not found")
return
}
let rootNC = UINavigationController(rootViewController: rootVC)
self.window?.rootViewController = rootNC
self.window?.makeKeyAndVisible()
}
[...]
在我的登录名中ViewController 我只有文本“FALSE”。 在 MainViewController 我有文本“TRUE” 当我尝试导航到 MainViewController(带箭头)时,它有效,但是当我尝试导航到 LoginViewController 时,我在控制台中输出:ViewController not found
故事板:
StoryboardsID 很好,因为我没有任何例外:
主要ViewController
登录ViewController
如何解决并导航到登录ViewController?
guard
子句 returns nil 因为您正在将具有 class LoginViewController
的 LoginVC 转换为 ViewController
- class 的 mainVC.
将guard let
改成这个
guard let rootVC = storyboard.instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController else {
print("ViewController not found")
return
}
你也能告诉我怎么让它更好看吗? 我只想在里面处理,不是一个外面一个里面。
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 windowScene = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: windowScene)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var rootVC = storyboard.instantiateViewController(withIdentifier: "MainViewController")
if (false) {
rootVC = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
}
let rootNC = UINavigationController(rootViewController: rootVC)
self.window?.rootViewController = rootNC
self.window?.makeKeyAndVisible()
}