0

我发现当在一个变量中使用变量时,.actionSheet变量在初始参与后没有更新。我已经尝试在主视图和提取视图中使用它,并且行为完全相同。在下图中,我点击了周二,但我显示的是周一的数据。这也使得在我需要传递数据的地方运行函数变得困难。

在此处输入图像描述

这是重现该问题的代码示例。

 var body: some View {
        VStack {
            ForEach(sampleSourceData) { link in
                VStack {
                    SampleView(day: link.day, distance: link.distance, exercise: link.exercise, showActionSheet: $showActionSheet)
                }
                .actionSheet(isPresented: $showActionSheet) {
                    ActionSheet(title: Text("\(link.day): \(link.distance) \(link.exercise)"),
                                message: Text("Stay Golden"),
                                buttons: [
                                    .cancel(),
                                    .default(
                                        Text("Action 1")
                                    ),
                                    .default(
                                        Text("Action 2")
                                    )
                                ]
                    )
                }
            }
        }
    }

样本数据和提取视图:

struct RunSourceSample: Identifiable {
    var id = UUID()
    var day: String
    var exercise: String
    var distance: String
}

var sampleSourceData = [
    RunSourceSample(day: "Monday", exercise: "Long run", distance: "6 Miles"),
    RunSourceSample(day: "Tuesday", exercise: "Tempo", distance: "9 Miles"),
    RunSourceSample(day: "Wednesday", exercise: "Rest", distance: ""),
    RunSourceSample(day: "Thursday", exercise: "Easy Run", distance: "4 Miles")
]

struct SampleView: View {
    var day: String
    var distance: String
    var exercise: String
    @Binding var showActionSheet: Bool

    var body: some View {
        HStack {
            Text(day)
            Text(distance)
            Text(exercise)
            Spacer()
            Button(action: {
                self.showActionSheet = true
            }) {
                Image(systemName: "ellipsis")
            }
        }
        .padding()
        .background(Color("BlueGray"))
        .cornerRadius(13)
    }
}
4

0 回答 0