首页 > 解决方案 > 我试图为我的应用程序实现启动画面,因为它需要从 api 将数据加载到应用程序中

问题描述

我正在开发一个将数据从 api 加载到应用程序中的应用程序。我试图通过在应用程序加载之前延迟 5 秒来加载 launchScreen。但它不起作用。

这是我正在使用的代码,我将启动屏幕 storyBorad 命名为“splashController”,将主故事板命名为“initController”。

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        self.splashScreen()
        return true
    }

    private func splashScreen() {
        self.window?.rootViewController = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateViewController(identifier: "splashController")
        self.window?.makeKeyAndVisible()
        print("Timer Started")
        Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(dismissSplashController), userInfo: nil, repeats: false)
    }

    @objc func dismissSplashController(){
        self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "initController")
        self.window?.makeKeyAndVisible()
    }

你能调查一下吗?

标签: iosswiftxcode

解决方案


您不应该延迟应用程序委托上的启动画面,您可以通过在您的第一个视图控制器中设计与启动画面设计相同的视图来实现此行为,并在成功获取主视图控制器中的数据时隐藏此视图


推荐阅读