我想通过View2Test
从View3Test
导航到RouterTest
。
我以解决问题的方式编写了演示代码。
我的问题是为什么我需要使用DispatchQueue.main.asyncAfter(deadline: .now() + 0.5)
来防止View3Test
打开后关闭,还是有更好的方法?
import SwiftUI
class RouterStore: ObservableObject {
static let shared = RouterStore()
@Published var showView1: Bool = false
@Published var showView3: Bool = false
}
struct RouterTest: View {
@ObservedObject var store = RouterStore.shared
var body: some View {
NavigationView {
VStack {
Text("RouterTest")
Button(
action: { self.store.showView1 = true },
label: { Text("Go to View1") }
)
NavigationLink(
destination: LazyView {
View3Test()
},
isActive: $store.showView3,
label: { EmptyView() }
)
NavigationLink(
destination: LazyView {
View1Test()
},
isActive: $store.showView1,
label: { EmptyView() }
)
.isDetailLink(false)
.onDisappear {
print("RouterTest onDisappear NavigationLink")
}
}
}
.onDisappear {
print("RouterTest onDisappear")
}
}
}
struct View1Test: View {
@ObservedObject var store = RouterStore.shared
var body: some View {
VStack {
Text("View1Test")
NavigationLink(
destination: LazyView {
View2Test()
},
label: { Text("Go to View2") }
)
}
}
}
struct View2Test: View {
@ObservedObject var store = RouterStore.shared
var body: some View {
VStack {
Text("View2Test")
Button(
action: {
self.store.showView1 = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.store.showView3 = true
}
},
label: { Text("Go to RouterTest") }
)
}
}
}
struct View3Test: View {
@ObservedObject var store = RouterStore.shared
var body: some View {
VStack {
Text("View3Test")
}
}
}
没有DispatchQueue.main.asyncAfter(deadline: .now() + 0.5)
答案 0 :(得分:0)
有更好的方法。我将self.store.showView3 = true
移至View2Test. onDisappear
是因为View2Test. onDisappear
在RouterTest.onAppear
之后被调用
//
// RouterTest.swift
// Traning2
//
// Created by Viktor Kushnerov on 8/26/20.
// Copyright © 2020 Viktor Kushnerov. All rights reserved.
//
import SwiftUI
class RouterStore: ObservableObject {
static let shared = RouterStore()
@Published var showView1: Bool = false
@Published var showView3: Bool = false
}
struct RouterTest: View {
@ObservedObject var store = RouterStore.shared
var body: some View {
NavigationView {
VStack {
Text("RouterTest")
Button(
action: { self.store.showView1 = true },
label: { Text("Go to View1") }
)
NavigationLink(
destination: LazyView {
View3Test()
},
isActive: $store.showView3,
label: { EmptyView() }
)
NavigationLink(
destination: LazyView {
View1Test()
},
isActive: $store.showView1,
label: { EmptyView() }
)
.isDetailLink(false)
.onDisappear {
print("RouterTest onDisappear NavigationLink")
}
}
}
.onDisappear {
print("RouterTest onDisappear")
}
}
}
struct View1Test: View {
@ObservedObject var store = RouterStore.shared
var body: some View {
VStack {
Text("View1Test")
NavigationLink(
destination: LazyView {
View2Test()
},
label: { Text("Go to View2") }
)
}
}
}
struct View2Test: View {
@ObservedObject var store = RouterStore.shared
var body: some View {
VStack {
Text("View2Test")
Button(
action: {
self.store.showView1 = false
},
label: { Text("Go to RouterTest") }
)
}
.onDisappear {
self.store.showView3 = true
}
}
}
struct View3Test: View {
@ObservedObject var store = RouterStore.shared
var body: some View {
VStack {
Text("View3Test")
}
}
}
struct RouterTest_Previews: PreviewProvider {
static var previews: some View {
RouterTest()
}
}