我正在尝试在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 }
}
我已经尝试过将最后一个警卫声明注释掉,也没有将其注释掉。
将其写入场景功能,并用视图控制器替换YOURCONTROLLER。
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
let mainstoryboard: UIStoryboard = UIStoryboard(name: "Home", bundle: nil)
let controller : YOURCONTROLLER = mainstoryboard.instantiateViewController(identifier: "YOURCONTROLLER") as! YOURCONTROLLER
let navigation = BaseNavigationController(rootViewController: controller)
window.rootViewController = navigation
self.window = window
window.makeKeyAndVisible()
}
您创建的窗口不正确,因此最终将出现黑屏。让情节提要为您创建窗口会更好,因为您不知道如何操作。只需完全删除此行:
self.window = UIWindow(frame: UIScreen.main.bounds)
您也可以剪切这行,因为它也是很简单的(情节提要也会为您完成此操作:]
self.window?.makeKeyAndVisible()
您现在唯一的责任是设置self.window?.rootViewController
的值。