swiftui - 如何使用 SwiftUI 在首次应用启动时启动教程?
问题描述
我想在我的 SwiftUI 应用程序首次启动时启动一个教程。这段代码应该在项目中的什么位置以及如何在应用程序首次启动时启动教程,它只是一个 SwiftUI 视图?
我已经知道如何在使用 UserDefaults 之前检查应用程序是否已启动。我想知道如何启动 SwiftUI 视图,然后在用户完成教程后如何启动标准 ContentView。
let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "hasLaunchedBefore")
if hasLaunchedBefore {
// Not first launch
// Load ContentView here
} else {
// Is first launch
// Load tutorial SwiftUI view here
UserDefaults.standard.set(true, forKey: "hasLaunchedBefore") // Set hasLaunchedBefore key to true
}
解决方案
试着把它放在你的场景中
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()
}
推荐阅读
- python - 在消息被调用后停止重复消息
- python - 如何从嵌套符号中提取字符串?
- java - IntelliJ 无法跨存储库解析 java.util 模块
- sql - 如何在 CASE 语句中使用“之间”?
- node.js - 使用不同的参数类型制作分页功能
- reactjs - AADSTS50011:请求中指定的回复 URL 与为应用程序配置的回复 URL 不匹配
- image - 突出显示选定的图像区域和操作 - Xamarin 表单
- cmake - 每次调用 make 时调用 add_custom_command
- cryptography - 使用币安美元购买美元
- c++ - 具有不同坐标系的屏幕外和屏幕上图像