使用 SwiftUI 关闭视图
Dismissing a view with SwiftUI
我想在用户第一次打开我的应用程序时显示一个说明页面。我已经让它工作了,但我不明白如何返回到 contentView(不重新启动应用程序)。
澄清一下:我希望 InstructionView 上的“关闭此视图”按钮将视图设置为显示为“ContentView”并关闭 InstructionView。现在 viewRouter.setToContentView() 使应用程序崩溃,我无法解决它。
提前致谢
TestDismissApp
import SwiftUI
@main
struct TestDismissApp: App {
var body: some Scene {
WindowGroup {
MotherView()
}
}
}
母景
import SwiftUI
struct MotherView : View {
@ObservedObject var viewRouter = ViewRouter()
var body: some View {
VStack {
if viewRouter.currentPage == "InstructionView" {
InstructionView()
} else if viewRouter.currentPage == "ContentView" {
ContentView()
}
}
}
}
ViewRouter
import Foundation
class ViewRouter: ObservableObject {
init() {
//UserDefaults.standard.set(false, forKey: "didLaunchBefore") remove // if you want to show the instructions again for test reasons
if !UserDefaults.standard.bool(forKey: "didLaunchBefore") {
UserDefaults.standard.set(true, forKey: "didLaunchBefore")
currentPage = "InstructionView"
} else {
currentPage = "ContentView"
}
}
func setToContentView () {
currentPage = "ContentView"
}
@Published var currentPage: String
}
内容视图
import SwiftUI
struct ContentView: View {
var body: some View {
Text("My wonderful content")
.padding()
}
}
InstructionView
import SwiftUI
struct InstructionView: View {
@Environment(\.dismiss) var dismiss
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
VStack {
Text("Instruction: This app shows a wonderful sentence")
Button {
viewRouter.setToContentView()
dismiss()
} label: {
Text("Dismiss this view")
}
}
}
}
试试这个:
在 MotherView
中使用 @StateObject var viewRouter = ViewRouter()
和
InstructionView().environmentObject(viewRouter)
我想在用户第一次打开我的应用程序时显示一个说明页面。我已经让它工作了,但我不明白如何返回到 contentView(不重新启动应用程序)。
澄清一下:我希望 InstructionView 上的“关闭此视图”按钮将视图设置为显示为“ContentView”并关闭 InstructionView。现在 viewRouter.setToContentView() 使应用程序崩溃,我无法解决它。
提前致谢
TestDismissApp
import SwiftUI
@main
struct TestDismissApp: App {
var body: some Scene {
WindowGroup {
MotherView()
}
}
}
母景
import SwiftUI
struct MotherView : View {
@ObservedObject var viewRouter = ViewRouter()
var body: some View {
VStack {
if viewRouter.currentPage == "InstructionView" {
InstructionView()
} else if viewRouter.currentPage == "ContentView" {
ContentView()
}
}
}
}
ViewRouter
import Foundation
class ViewRouter: ObservableObject {
init() {
//UserDefaults.standard.set(false, forKey: "didLaunchBefore") remove // if you want to show the instructions again for test reasons
if !UserDefaults.standard.bool(forKey: "didLaunchBefore") {
UserDefaults.standard.set(true, forKey: "didLaunchBefore")
currentPage = "InstructionView"
} else {
currentPage = "ContentView"
}
}
func setToContentView () {
currentPage = "ContentView"
}
@Published var currentPage: String
}
内容视图
import SwiftUI
struct ContentView: View {
var body: some View {
Text("My wonderful content")
.padding()
}
}
InstructionView
import SwiftUI
struct InstructionView: View {
@Environment(\.dismiss) var dismiss
@EnvironmentObject var viewRouter: ViewRouter
var body: some View {
VStack {
Text("Instruction: This app shows a wonderful sentence")
Button {
viewRouter.setToContentView()
dismiss()
} label: {
Text("Dismiss this view")
}
}
}
}
试试这个:
在 MotherView
中使用 @StateObject var viewRouter = ViewRouter()
和
InstructionView().environmentObject(viewRouter)