简体   繁体   English

NavigationLink.navigationDestination 多次调用并推送到新视图两次

[英]NavigationLink .navigationDestination called multiple times and pushes to new View twice

I have a searchable list displaying various instances of a certain Data Model which are saved in an array.我有一个可搜索列表,显示保存在数组中的特定数据 Model 的各种实例。 When tapping on one of the list rows a new view opens displaying some information about the Data Model. For demonstration purposes, the view opening displays a randomly generated Number.当点击列表行之一时,会打开一个新视图,显示有关数据 Model 的一些信息。出于演示目的,打开的视图会显示一个随机生成的数字。 This works as expected up to this point.到目前为止,这按预期工作。

However, when performing a search displaying multiple items and then performing a second search displaying only a subset of the first items, after tapping on the selected row, the NavigationLink pushes to the new view twice.但是,当执行显示多个项目的搜索然后执行仅显示第一个项目的子集的第二次搜索时,在点击所选行后,NavigationLink 会两次推送到新视图。

This problem is easy to replicate with very little code.这个问题很容易用很少的代码复制。 Here is my Data Model:这是我的数据 Model:

struct DataModel: Identifiable, Hashable {
let id = UUID()
var name: String

init(name: String = "unknown") {
    self.name = name
}

static func == (lhs: DataModel, rhs: DataModel) -> Bool {
    return lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
    hasher.combine(id)
}
}

And here is my View:这是我的观点:

var allDataModels = [DataModel]()

struct ContentView: View {
@State var searchDataModels = [DataModel]()
@State var searchText = ""

let numbers = Array(1...10)

var body: some View {
    NavigationStack {
        List {
            ForEach(searchDataModels, id: \.id) { model in
                NavigationLink(value: model, label: {
                    Text(model.name)
                })
            }
        }.searchable(text: $searchText)

            .onChange(of: searchText, perform: { _ in
                updateSearch()
            })
            
            .navigationDestination(for: DataModel.self, destination: { _ in
                Text("\(Int.random(in: 1...100))")})
    }.onAppear {
        for i in 0...9 {
            allDataModels.append(DataModel(name: "Data \(numbers[i])"))
        }
    }
}

func updateSearch() {
    searchDataModels = allDataModels.filter( { $0.name.localizedCaseInsensitiveContains(searchText) } )
}
}

The following video demonstrates the NavigationLink pushing to two Views after performing two searches.以下视频演示了 NavigationLink 在执行两次搜索后推送到两个视图。 As is visible in the video, the numbers on the screen change, making the views easy to distinguish from one another.正如视频中所见,屏幕上的数字发生了变化,使得视图之间很容易区分。

问题可视化

This Problem has been tested and occurs in iOS 16 beta 3 and persists in beta 4. Earlier versions were not tested (NavigationStack and .navigationDestination(for: , destination:) are new in iOS 16).此问题已经过测试,出现在 iOS 16 beta 3 中,并持续存在于 beta 4 中。早期版本未经过测试(NavigationStack 和.navigationDestination(for: , destination:)是 iOS 16 中的新增内容)。

The problem seems to be resolved in iOS 16 beta 5.这个问题似乎在 iOS 16 beta 5 中得到了解决。

I am having the same problem in Xcode 14.2.我在 Xcode 14.2 中遇到了同样的问题。 I have NavigationStack with List showing CoreData from @FetchRequest.我有带有列表的 NavigationStack,显示来自@FetchRequest 的 CoreData。 Strange thing is that it started to behave like this when I started storing Binary Data.奇怪的是,当我开始存储二进制数据时,它开始表现得像这样。 Not sure if it's just coinsidence.不确定这是否只是巧合。 For the moment, I am "happy" with a "workaround" that I found at:目前,我对在以下位置找到的“解决方法”感到“满意”:

https://developer.apple.com/forums/thread/711899?answerId=725008022#725008022 https://developer.apple.com/forums/thread/711899?answerId=725008022#725008022

All text below is copied from the before mentioned link.下面的所有文本都是从前面提到的链接复制的。

With Form.buttonStyle(BorderlessButtonStyle()) on the Stack or Button fixed the Issue.使用 Stack 或 Button 上的 Form.buttonStyle(BorderlessButtonStyle()) 修复了问题。

I think the Error comes from a kinda hit-test Layer problem with the Default Button in some Cases of Navigation Stack or Form.我认为错误来自在某些导航堆栈或表单的情况下默认按钮的命中测试层问题。 In my Form, without the modifier, all Buttons in a Stack are hit together.在我的表单中,如果没有修饰符,堆栈中的所有按钮都会一起点击。 The modifier fixed it just by accident.修改器只是偶然修复了它。 On runtime, you just don't get the correct root of the Error.在运行时,您只是没有得到正确的错误根源。 It's a strange Bug indeed这确实是一个奇怪的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM