为什么弹出到root在这个示例代码SwiftUI中不起作用?

  时间:2022-05-11 07:07:02  阅读量:18  评论数:0  作者:Just a coder

为了解决这个问题,我提供了最少的示例代码来重新创建bug。只需复制/粘贴它即可运行。

import SwiftUI

final class Popper: ObservableObject {
    @Published var shouldProceed: String? = nil
    var id: String
    init(id: String) { self.id = id }
}

struct ContentView: View {
    @StateObject var popper = Popper(id: "ROOT")
    
    var body: some View {
        NavigationView {
            VStack(spacing: 40) {
                Text("You are now in ROOT").font(.largeTitle)
                Text("Tap Here to goto V1").onTapGesture {
                    popper.shouldProceed = "y"
                }.foregroundColor(.blue)
                NavigationLink(destination: V1().environmentObject(popper),
                               tag: "y",
                               selection: $popper.shouldProceed) {
                    EmptyView()
                }
            }
        }
    }
}

struct V1: View {
    @EnvironmentObject var rootPopper: Popper
    @StateObject var v1Popper = Popper(id: "V1")
    var body: some View {
        VStack(spacing: 40) {
            Text("You are now in V1").font(.largeTitle)
            Text("Tap Here to pop to root").onTapGesture {
                print("popping to: \(rootPopper.id)")
                rootPopper.shouldProceed = nil
            }.foregroundColor(.red)
            
            Text("Or tap here to go to V2").onTapGesture {
                v1Popper.shouldProceed = "y"
            }.foregroundColor(.blue)
            NavigationLink(destination: V2().environmentObject(v1Popper),
                           tag: "y",
                           selection: $v1Popper.shouldProceed) {
                EmptyView()
            }
            
        }
    }
}

struct V2: View {
    @EnvironmentObject var v1Popper: Popper
    @State var shouldProceed: String? = nil
    var body: some View {
        VStack(spacing: 40) {
            Text("You are now in V2").font(.largeTitle)
            Text("Tap Here to pop to V1").onTapGesture {
                print("popping to: \(v1Popper.id)")
                v1Popper.shouldProceed = nil
            }.foregroundColor(.red)
            
            Text("Or Tap here to gotoV3").onTapGesture {
                shouldProceed = "y"
            }.foregroundColor(.blue)
            NavigationLink(destination: V3().environmentObject(v1Popper),
                           tag: "y", selection: $shouldProceed) {
                EmptyView()
            }
            
        }
    }
}

struct V3: View {
    @EnvironmentObject var v1Popper: Popper
    var body: some View {
        VStack(spacing: 40) {
            Text("You are now in V3").font(.largeTitle)
            
            Text("Tap Here to pop to V1").onTapGesture {
                print("popping to: \(v1Popper.id)")
                v1Popper.shouldProceed = nil
            }.foregroundColor(.red)
        }
    }
}

问题当你到达V3屏幕时,为什么它不弹到V1屏幕?所有其他pop函数都可以工作。当它到达屏幕V3时,它只是由于某种原因而失败。 请帮忙。

网友解决方案:

尝试使用.isdetaillink(false)作为链接,比如

NavigationLink(destination: V1().environmentObject(popper),
               tag: "y",
               selection: $popper.shouldProceed) {
    EmptyView()
}.isDetailLink(false)     // << here !!

原文地址(source):原文链接