0

我想从 导航View2TestView3Testthrough 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)

在此处输入图像描述

4

1 回答 1

0

有更好的方法。我搬去self.store.showView3 = trueView2Test. 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()
    }
}
于 2020-08-28T10:57:34.590 回答