我以编程方式设置的初始视图未加载

My Programatically set initial view is not loading

我通过登录键从 appdelegate 设置了一个初始视图控制器

即使我的登录密钥是真的,它也没有加载我的主页选项卡视图控制器,它每次都加载启动屏幕,然后是登录视图控制器。我通过用户默认值检查了我的登录视图密钥更改,它正确显示了值。 当我从故事板 IB 明智地更改任何视图控制器时,它正在设置,但我的代码不起作用。 我的代码有什么问题?

    if isLogin == false
           {
               self.window = UIWindow(frame: UIScreen.main.bounds)
               let storyboard = UIStoryboard(name: "Main", bundle: nil)
               let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginVC")
               self.window?.rootViewController = initialViewController
               self.window?.makeKeyAndVisible()
           }
           else
           {
               self.window = UIWindow(frame: UIScreen.main.bounds)
               let storyboard = UIStoryboard(name: "Main", bundle: nil)
               let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarVC")
               self.window?.rootViewController = initialViewController
               self.window?.makeKeyAndVisible()
           }

如果您在 ios 13 或以上,则需要使用 UIWindowSceneDelegate。使用以下方法在 SceneDelegate 文件

中初始化您的应用程序
import UIKit

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 _ = (scene as? UIWindowScene) else { return }

        if isLogin == false
        {
            self.window = UIWindow(frame: UIScreen.main.bounds)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginVC")
            self.window?.rootViewController = initialViewController
            self.window?.makeKeyAndVisible()
        }
        else
        {
            self.window = UIWindow(frame: UIScreen.main.bounds)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarVC")
            self.window?.rootViewController = initialViewController
            self.window?.makeKeyAndVisible()
        }
    }
}

AppDelegate 文件,

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        if #available(iOS 13.0, *) {

        } else {
            if isLogin == false
            {
                self.window = UIWindow(frame: UIScreen.main.bounds)
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginVC")
                self.window?.rootViewController = initialViewController
                self.window?.makeKeyAndVisible()
            }
            else
            {
                self.window = UIWindow(frame: UIScreen.main.bounds)
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let initialViewController = storyboard.instantiateViewController(withIdentifier: "TabBarVC")
                self.window?.rootViewController = initialViewController
                self.window?.makeKeyAndVisible()
            }
        }
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
}

ios13以下,不需要像self.window = UIWindow(frame: UIScreen.main.bounds)那样初始化window。只需使用该应用程序自动在 AppDelegate.

中创建 window 属性

在 AppDelegate 中

例如:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let vc = ViewController()
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.rootViewController = vc
    window?.makeKeyAndVisible()
    return true
}

你能检查一下你没有在函数后设置 window 吗?

我认为有两个可能的原因:

  1. isLogin 始终为 false

  2. 你设置了两次window(可能在你的函数之后)