Есть ли лучший способ открыть другое представление через корневое представление?
Я бы хотел перейти от 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)
1 ответ
Есть способ лучше. я переехал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()
}
}