如何在iOS13中使用Scene Delegate启用Onboarding? [英] How to make Onboarding work with Scene Delegate in iOS13?

查看:354
本文介绍了如何在iOS13中使用Scene Delegate启用Onboarding?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SceneDelegate中设置我的入职屏幕.

I am trying to set up my onboarding screen in the SceneDelegate.

当我运行下面的代码时,它会编译,但只会进入黑屏.

When I run the code below, it compiles, but just goes to a black screen.

对于AppDelegate来说,它们是许多很棒的入门教程,但对于带有iOS13的新SceneDelegate,却很少.我上了本教程,并尝试将其应用于SceneDelegate,但无法正常工作:

They're many great onboarding tutorials for AppDelegate, but very few for the new SceneDelegate with iOS13. I took this tutorial and tried to apply it to SceneDelegate, but I can't get it to work: https://www.youtube.com/watch?v=y6t1woVd6RQ&t=537s

这是我的场景委托代码.

This is my scene delegate code.

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let launchedBefore = UserDefaults.standard.bool(forKey: "hasLaunched")
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let launchStoryboard = UIStoryboard(name: "Onboarding", bundle: nil)
        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        var vc: UIViewController
        if launchedBefore
        {
            vc = mainStoryboard.instantiateInitialViewController()!
        }
        else
        {
            vc = launchStoryboard.instantiateViewController(identifier: "Onboarding")
        }
        UserDefaults.standard.set(true, forKey: "hasLaunched")
        self.window?.rootViewController = vc
        self.window?.makeKeyAndVisible()

    //    guard let _ = (scene as? UIWindowScene) else { return }
    }

我已经尝试过注释掉最后一个警卫声明并且不注释掉它.

I've tried it both with commenting out the last guard statement and with not commenting it out.

推荐答案

您创建的窗口不正确,因此最终将出现黑屏.让情节提要为您创建窗口会更好,因为您不知道如何操作.只需完全删除此行:

You're creating the window incorrectly, so you're going to end up with a black screen. It would be a lot better to let the storyboard create the window for you, since you don't know how to do it. Just delete this line completely:

self.window = UIWindow(frame: UIScreen.main.bounds)

您还可以剪切此行,因为它也是很简单的(情节提要也会为您完成此操作):

You can also cut this line, as it is also otiose (the storyboard will do this for you as well):

self.window?.makeKeyAndVisible()

您现在的唯一职责是设置self.window?.rootViewController的值.请注意,您不必说

Your sole responsibility now is to set the value of self.window?.rootViewController. Note that you do not need to say

vc = mainStoryboard.instantiateInitialViewController()!

因为是情节提要提供给您的根视图控制器.因此,在您已放置的体系结构中, 唯一需要做的就是在需要加入用户的情况下更换根视图控制器.

because that is the root view controller already, given to you by the storyboard. Thus the only thing you need to do, in the architecture you've posited, is replace the root view controller in the situation where the user needs to be onboarded.

这篇关于如何在iOS13中使用Scene Delegate启用Onboarding?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆