【问题标题】:Navigation repeats itself several times after clicking the object单击对象后导航会重复多次
【发布时间】:2021-03-25 20:28:23
【问题描述】:

我刚刚与 Apple 分享了这个错误。我想和你分享。

应用关注

1 - 用户登录到 onBoardingView 页面后,他们将被定向到具有 fullScreenCover 的 ContentView。

2 - ContentView 页面包含 TabView 中与 ForEach 重复的对象。单击这些对象将带您进入 DetailView 页面。

3 - 但是,导航在单击对象后会重复多次。

我的英语不好。对此感到抱歉。

视频是here

项目文件为here

struct OnboardView: View {
    @State var isLogin: Bool = false
    var body: some View {
        
        Button(action: {self.isLogin = true}) {
            Text("Login")
        }
        .fullScreenCover(isPresented: self.$isLogin) {
            ContentView()
        }
    }
}


struct ContentView: View {
    @State var selected: String = ""
    var items: [String] = ["1","2","3","4","5","6","7","8","9","10"]
    var body: some View {
        NavigationView {
            TabView(selection: $selected) {
                ForEach(items, id: \.self) { item in
                    NavigationLink(
                        destination: DetailView(),
                        label: {
                            Text(item)
                                .foregroundColor(.white)
                                .padding()
                                .background(Color.orange)
                                .cornerRadius(10)
                        })
                }
            }
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
            .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
        }
    }
}

【问题讨论】:

    标签: swiftui tabview bug-reporting pagetabviewstyle


    【解决方案1】:

    在 SwiftUI 中使用 ForEach 时,您必须格外小心 id。

    尝试将项目改为items.indices

                    ForEach(items.indices, id: \.self) { item in
                        NavigationLink(
                            destination: Text("Detail View"),
                            label: {
                                Text(items[item])
                                    .foregroundColor(.white)
                                    .padding()
                                    .background(Color.orange)
                                    .cornerRadius(10)
                            }
                        )
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 2020-06-26
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多