Swiftui NavigationLink标记和选择未按预期工作

Swiftui NavigationLink标记和选择未按预期工作,swiftui,ios-navigationview,swiftui-14.2-defect,Swiftui,Ios Navigationview,Swiftui 14.2 Defect,我可能遗漏了一些东西,但我无法使NavigationLink在列表中工作 我正在使用NavigationLink(destination、tag、selection),我想点击按钮弹出到根视图,正如您在本示例项目中看到的: import SwiftUI struct ContentView: View { @State var selectedView: Int? = nil var colors: [String] = ["blue", "yel

我可能遗漏了一些东西,但我无法使NavigationLink在列表中工作
我正在使用NavigationLink(destination、tag、selection),我想点击按钮弹出到根视图,正如您在本示例项目中看到的:

import SwiftUI

struct ContentView: View {
    @State var selectedView: Int? = nil
    var colors: [String] = ["blue", "yellow", "green", "red", "black"]
    
    var body: some View {
        NavigationView {
            List{
                ForEach(colors.indices) { index in
                    NavigationLink(
                        destination: ColorDetail(selectedView: self.$selectedView, color: colors[index]),
                        tag: index,
                        selection: self.$selectedView,
                        label: {
                            Text(colors[index])
                        })
                }
            }
        }
    }
}

struct ColorDetail: View {
    
    @Binding var selectedView: Int?
    var color: String
    
    var body: some View {
        VStack {
            Text(color)
            Text("SelectedView: \(selectedView ?? 99)")
            Button("set SelectedView to nil and go back") {
                self.selectedView = nil
            }
        }
    }
}
为什么如果我将selectedView设置为零,什么都不会发生?如何通过点击按钮从ColorDetail弹出到根视图(ContentVIew)


只要复制这段代码并尝试一下,它就会生成。

正如Asperi所说,它看起来像是14.2中的一个bug,因为它真的可以将其设置为零。但是,如果您需要一种适用于所有版本的变通方法,请尝试使用
presentationMode

struct ColorDetail: View {
    
    @Binding var selectedView: Int?
    var color: String
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        VStack {
            Text(color)
            Text("SelectedView: \(selectedView ?? 99)")
            Button("set SelectedView to nil and go back") {
                self.selectedView = nil
                self.presentationMode.wrappedValue.dismiss()
            }
        }
    }
}

另一个14.2缺陷。14.1.0版也可以,真的吗?我希望我做错了什么。。。