iOS13 如何让 Onboarding 与 Scene Delegate 一起工作?
How to make Onboarding work with Scene Delegate in iOS13?
我正在尝试在 SceneDelegate 中设置我的引导屏幕。
当我 运行 下面的代码时,它编译了,但只是进入黑屏。
他们有很多很棒的 AppDelegate 入门教程,但很少有 iOS13 的新 SceneDelegate。我学习了本教程并尝试将其应用于 SceneDelegate,但我无法让它工作:https://www.youtube.com/watch?v=y6t1woVd6RQ&t=537s
这是我的场景委托代码。
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 }
}
我已经尝试过注释掉最后一个 guard 语句和不注释掉它。
您创建的 window 不正确,因此最终会出现黑屏。让情节提要为您创建 window 会好得多,因为您不知道该怎么做。完全删除这一行:
self.window = UIWindow(frame: UIScreen.main.bounds)
你也可以剪掉这行,因为它也很麻烦(故事板也会为你做这件事):
self.window?.makeKeyAndVisible()
您现在唯一的责任就是设置 self.window?.rootViewController
的值。请注意,您不需要说
vc = mainStoryboard.instantiateInitialViewController()!
因为故事板已经为您提供了根视图控制器。因此,在您提出的架构中,您 唯一需要做的就是在用户需要入职的情况下替换根视图控制器。
(有关工作示例,请参阅 my github repo。)
matt 解决了这个问题,请看他的回答。
只需在下面为任何试图在 ios13 中使用故事板进行入职培训的人发布正确的代码。
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let launchedBefore = UserDefaults.standard.bool(forKey: "hasLaunched")
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: "OnboardingScene")
}
UserDefaults.standard.set(true, forKey: "hasLaunched")
self.window?.rootViewController = vc
}
我正在尝试在 SceneDelegate 中设置我的引导屏幕。
当我 运行 下面的代码时,它编译了,但只是进入黑屏。
他们有很多很棒的 AppDelegate 入门教程,但很少有 iOS13 的新 SceneDelegate。我学习了本教程并尝试将其应用于 SceneDelegate,但我无法让它工作:https://www.youtube.com/watch?v=y6t1woVd6RQ&t=537s
这是我的场景委托代码。
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 }
}
我已经尝试过注释掉最后一个 guard 语句和不注释掉它。
您创建的 window 不正确,因此最终会出现黑屏。让情节提要为您创建 window 会好得多,因为您不知道该怎么做。完全删除这一行:
self.window = UIWindow(frame: UIScreen.main.bounds)
你也可以剪掉这行,因为它也很麻烦(故事板也会为你做这件事):
self.window?.makeKeyAndVisible()
您现在唯一的责任就是设置 self.window?.rootViewController
的值。请注意,您不需要说
vc = mainStoryboard.instantiateInitialViewController()!
因为故事板已经为您提供了根视图控制器。因此,在您提出的架构中,您 唯一需要做的就是在用户需要入职的情况下替换根视图控制器。
(有关工作示例,请参阅 my github repo。)
matt 解决了这个问题,请看他的回答。
只需在下面为任何试图在 ios13 中使用故事板进行入职培训的人发布正确的代码。
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let launchedBefore = UserDefaults.standard.bool(forKey: "hasLaunched")
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: "OnboardingScene")
}
UserDefaults.standard.set(true, forKey: "hasLaunched")
self.window?.rootViewController = vc
}