不一致的 ContextMenu 前向选择

Inconsistent ContextMenu forward selection

我正在尝试构建一个 ContextMenu 以允许应用程序用户在长按其中一项锻炼时从其 collection 中编辑或删除现有锻炼,但锻炼选择不一致在编辑和删除操作之间。

let workouts = workoutManager.getAllWorkoutsFromCollection()
if workouts.count > 0 {
    ForEach(workouts) { workout in
        WorkoutCardView(workout: workout)
            .contextMenu {
                Button {
                    print("Edit Workout: \(workout.title)")     // for debug purpose
                    
                    showEditWorkoutSheet.toggle()
                } label: {
                    Label("Edit Workout", systemImage: "square.and.pencil")
                }
                
                Button(role: .destructive) {
                    print("Delete Workout: \(workout.title)")   // for debug purpose
                    
                    workoutManager.removeWorkoutFromCollection(workout)
                } label: {
                    Label("Delete Workout", systemImage: "trash")
                }
            }
            .sheet(isPresented: $showEditWorkoutSheet) {
                AWSheetView(workout: workout, editWorkout: true)
            }
    }
} 

workoutManager.getAllWorkoutsFromCollection() returns 所有锻炼作为锻炼自定义数据类型的数组。

func removeWorkoutFromCollection(_ workout: Workout) {
    print("WorkoutManager::removeWorkoutFromCollection: \(workout.title)")  // for  debug purpose
    
    workouts.removeAll(where: { [=11=].id == workout.id } )
}

func updateWorkout(_ workout: Workout) {
    if let index = workouts.firstIndex(where: { [=11=].id == workout.id } ) {
        print("WorkoutManager::updateWorkout: \(workout.title)")            // for  debug purpose
        
        workouts[index] = workout
    }
}

里面 AWSheetView 我有以下内容:

@State var workout: Workout = Workout()
var editWorkout: Bool = false

我也在此处构建一个空的 Workout,因为我正在使用 AWSsheetView 将新的 Workout 添加到 collection,而且还根据 [=20] 编辑现有锻炼=]变量。

当长按锻炼并选择删除时,所选锻炼将从 collection 中删除,我有以下控制台输出:

Delete Workout: Legs Day
WorkoutManager::removeWorkoutFromCollection: Legs Day

但是当按下“编辑”时,输出如下所示,并且不知何故总是在 AWSsheetView 中收到第一个锻炼而不是选定的锻炼:

Edit Workout: Legs Day
WorkoutManager::updateWorkout: Upper body

有谁知道我在这里做错了什么?我试了好几天都没有结果。

下面是2张截图。在第二个屏幕截图中,您可以看到在“腿部日”锻炼中按 编辑AWSheetView 的样子。

美腿日长按:

在“腿部日”锻炼中按“编辑”的结果:

Asperi (check here the solution) 指出的解决方案非常有效。

这是基于上述link的新实现:

@State private var selectedWorkout: Workout? = nil

let workouts = workoutManager.getAllWorkoutsFromCollection()
if workouts.count > 0 {
    ForEach(workouts) { workout in
        WorkoutCardView(workout: workout)
            .contextMenu {
                Button {
                    print("Edit Workout: \(workout.title)")     // for debug purpose
                    
                    selectedWorkout = workout
                } label: {
                    Label("Edit Workout", systemImage: "square.and.pencil")
                }
                
                Button(role: .destructive) {
                    print("Delete Workout: \(workout.title)")   // for debug purpose
                    
                    workoutManager.removeWorkoutFromCollection(workout)
                } label: {
                    Label("Delete Workout", systemImage: "trash")
                }
            }
    }
    .sheet(item: self.$selectedWorkout) { selectedWorkout in
        AWSheetView(workout: selectedWorkout, editWorkout: true)
    }
} else {
    Text("No workouts found.")
        .font(.system(size: 16))
        .foregroundColor(.red)
}