为什么在此示例代码 SwiftUI 中无法弹出根目录? [英] Why pop to root does not work in this sample code SwiftUI?

查看:23
本文介绍了为什么在此示例代码 SwiftUI 中无法弹出根目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于这个问题的目的,我提供了最少的示例代码来重新创建错误.只需复制/粘贴它即可运行.

For the purpose of this question, I have provided minimum sample code to re-create the bug. Just copy/paste it to run.

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 屏幕?所有其他弹出功能都有效.当它进入屏幕 V3 时,它只是出于某种原因失败了.请帮忙.

Question When you get to screen V3, why does it not pop to the V1 screen? All the other pop functions work. It just fails for some reason when it gets to screen V3. Please help.

推荐答案

尝试使用 .isDetailLink(false) 作为链接,例如

Try to use .isDetailLink(false) for your links, like

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

这篇关于为什么在此示例代码 SwiftUI 中无法弹出根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆