【问题标题】:My Programatically set initial view is not loading [duplicate]我以编程方式设置的初始视图未加载[重复]
【发布时间】:2020-05-16 12:47:59
【问题描述】:

我通过登录密钥从 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()
           }

【问题讨论】:

  • 尝试使用“instantiateInitialViewController()”而不是“instantiateViewController()”
  • 与您的问题无关,但由于您的流程中唯一改变的是控制器标识符,您可以简化 if/else 以仅返回不同的标识符(而不是复制相同的代码)

标签: ios swift appdelegate


【解决方案1】:

如果您使用的是 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.
    }
}

对于ios 13以下,不需要像self.window = UIWindow(frame: UIScreen.main.bounds)那样初始化窗口。只需使用该应用程序自动在AppDelegate 中创建窗口属性。

【讨论】:

  • 在应用程序委托中对吗?如果我更改为此方法,如果设备运行低于 ios 13 怎么办
  • @Fido 请检查更新的答案
  • 我检查了场景委托不工作。启动后屏幕变黑
  • 我在模拟器上测试这个
  • @Fido 我已经添加了两个文件的代码,请检查。
【解决方案2】:

在 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
}

你能检查一下你没有在你的函数之后设置窗口吗?

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

  1. isLogin 始终为假

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

【讨论】:

  • 我的登录密钥是真的。我打印了。但是改成你的代码后还是一样的问题
  • @Fido 你能分享你的项目吗?
猜你喜欢
  • 1970-01-01
  • 2012-05-12
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多