【发布时间】:2020-02-22 15:26:17
【问题描述】:
我对此很陌生,但遇到了问题。我的目标是制作一个欢迎您使用该应用程序的入职屏幕。我希望它在第一次启动应用程序后消失。我关注了这个tutorial。我没有将代码放在AppDelegate 中,而是将代码放在SceneDelegate 下:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
...
这是我到目前为止的代码:
导入 UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
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: "Main", bundle: nil)
let mainStoryboard = UIStoryboard(name: "actual", bundle: nil)
var vc: UIViewController
if launchedBefore
{
vc = mainStoryboard.instantiateInitialViewController()!
}
else
{
vc = launchStoryboard.instantiateViewController(identifier: "firststoryboard")
}
UserDefaults.standard.set(true, forKey: "hasLaunched")
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
// 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 }
}
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
}
现在我没有收到任何错误,但是当我启动应用程序时屏幕是黑色的。有谁知道我做错了什么?
【问题讨论】:
-
您将代码放入场景委托中真是很棒。但是您没有向我们显示代码,因此我们不知道您可能做错了什么。如果您需要帮助,请显示您的代码。总是。这是适用于 iOS 12 和 iOS 13 的可下载示例:github.com/mattneub/RegistrationExample/blob/master/…
-
嗨对不起!忘了把重要的部分放进去。我现在编辑了我的消息
-
删除此行:
self.window? = UIWindow(frame: UIScreen.main.bounds)。这不是在场景委托中创建窗口的正确方法。