Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SwiftUI启动第一个应用程序的教程?_Swiftui - Fatal编程技术网

如何使用SwiftUI启动第一个应用程序的教程?

如何使用SwiftUI启动第一个应用程序的教程?,swiftui,Swiftui,我想在我的SwiftUI应用程序首次启动时启动一个教程。这段代码应该放在项目中的什么地方?当应用程序第一次启动时,我如何启动教程(这只是一个SwiftUI视图) 在使用UserDefaults之前,我已经知道如何检查应用程序是否已启动。我想知道如何启动SwiftUI视图,然后在用户完成教程后如何启动标准ContentView 让hasLaunchedBefore=UserDefaults.standard.bool(forKey:“hasLaunchedBefore”) 如果在发射之前{ //不

我想在我的SwiftUI应用程序首次启动时启动一个教程。这段代码应该放在项目中的什么地方?当应用程序第一次启动时,我如何启动教程(这只是一个SwiftUI视图)

在使用UserDefaults之前,我已经知道如何检查应用程序是否已启动。我想知道如何启动SwiftUI视图,然后在用户完成教程后如何启动标准ContentView

让hasLaunchedBefore=UserDefaults.standard.bool(forKey:“hasLaunchedBefore”)
如果在发射之前{
//不是第一次发射
//在此处加载ContentView
}否则{
//这是第一次发射
//在此处加载教程SwiftUI视图
UserDefaults.standard.set(true,forKey:“hasLaunchedBefore”)//将hasLaunchedBefore键设置为true
}

试着把这个放到你的sceneDelegate中

let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "hasLaunchedBefore")
let content = ContentView()
let tutorial = TutorialView()
if let windowScene = scene as? UIWindowScene {
      let window = UIWindow(windowScene: windowScene)
       if hasLaunchedBefore {
           window.rootViewController = UIHostingController(rootView: content)
       } else {
           window.rootViewController = UIHostingController(rootView: tutorial)
           UserDefaults.standard.set(true, forKey: "hasLaunchedBefore") 
       }
        self.window = window
        window.makeKeyAndVisible()
}

这在第一次启动时起作用,但如果我在启动后关闭应用程序并重新打开它,它就会崩溃。不过,我忘了将我的一个环境对象添加回内容视图。谢谢你的帮助!