【问题标题】:Why pop to root does not work in this sample code SwiftUI?为什么 pop to root 在这个示例代码 SwiftUI 中不起作用?
【发布时间】:2020-09-24 18:02:56
【问题描述】:

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

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 时,它只是由于某种原因而失败。 请帮忙。

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    尝试在链接中使用.isDetailLink(false),例如

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

    【讨论】:

    • 我没有看到关于为什么这样做的文档。你有解释吗?
    • 真的没人解释吗?
    • @FlowUI.SimpleUITesting.com,如果您寻找为什么的官方答案,那么没有人。根据经验,此标志会影响内部链接堆栈的构建方式 - 它要么是平坦的 (isDetailLink ==false),然后所有标签都位于一个空间中 - 因此解决方案有效,要么不平坦,然后每个显示的链接都会引入新的标签空间,我们观察原始问题中的行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多