0

Alert 应该从模型中删除对象,但是当 Alert 出现时,值与 tap 和 longPress Gesture 中的值不同。知道为什么吗?

LazyVGrid(columns: columns, spacing: 5) {
            ForEach(model.robotsRepository.robots) { robot in
                RobotView(model: RobotView.Model(robotsRepository: model.robotsRepository, robot: robot)).environmentObject(team)
                    .contentShape(Rectangle())
                    .alert(isPresented: $showAlert) {
                        //When Alert is apearing, "robot" value are not the same as a value inside tap and longPress Gesture
                        Alert(
                            title: Text("Remove Card"),
                            message: Text("Are you sure you want to remove this card?"),
                            primaryButton: .destructive(Text("Remove")) {
                                withAnimation {
                                    model.remove(robot: robot)
                                }
                            },
                            secondaryButton: .cancel() {
                                withAnimation {
                                    deleting = false
                                }
                            }
                        )
                    }
                    .onTapGesture {
                        addRobot(robot: robot, at: selectedIndex)
                    }
                    .onLongPressGesture {
                        withAnimation {
                            showAlert = true
                            deleting = true
                        }
                    }
            }
        }
4

1 回答 1

0

您可以尝试将.alert(isPresented: $showAlert) {...}移出ForEach,也可能移出LazyVGrid。像这样的东西,例如(未经测试):

@State var selectedRobot = Robot.default  // <--- here adjust accordingly

LazyVGrid(columns: columns, spacing: 5) {
            ForEach(model.robotsRepository.robots) { robot in
                RobotView(model: RobotView.Model(robotsRepository: model.robotsRepository, robot: robot)).environmentObject(team)
                    .contentShape(Rectangle())
                    .onTapGesture {
                        addRobot(robot: robot, at: selectedIndex)
                    }
                    .onLongPressGesture {
                        withAnimation {
                            selectedRobot = robot  // <-- here
                            showAlert = true
                            deleting = true
                        }
                    }
            }
        }
        .alert(isPresented: $showAlert) {
            Alert(
                title: Text("Remove Card"),
                message: Text("Are you sure you want to remove this card?"),
                primaryButton: .destructive(Text("Remove")) {
                    withAnimation {
                        model.remove(robot: selectedRobot) // <-- here
                    }
                },
                secondaryButton: .cancel() {
                    withAnimation {
                        deleting = false
                    }
                }
            )
        }
     

EDIT-1:请注意alert(isPresented:content:) alert(item:content:)已弃用(iOS 13.0–15.4 )。

如果您使用的是(ios15+),您可以使用以下方法(根据 Apple 示例:https ://developer.apple.com/documentation/swiftui/view/alert(_:ispresented:presenting:actions:message:)- 8584l )

// for demonstration
struct Robot: Identifiable {
    let id = UUID()
    let name: String = "no name"
}

.....

@State var selectedRobot: Robot?  // <--- here

 .....

        LazyVGrid(columns: columns, spacing: 5) {
            ForEach(model.robotsRepository.robots) { robot in
                RobotView(model: RobotView.Model(robotsRepository: model.robotsRepository, robot: robot)).environmentObject(team)
                    .contentShape(Rectangle())
                    .onTapGesture {
                        addRobot(robot: robot, at: selectedIndex)
                    }
                    .onLongPressGesture {
                        withAnimation {
                            selectedRobot = robot  // <-- here
                            showAlert = true
                            deleting = true
                        }
                    }
            }
        }
        .alert("Remove Card", isPresented: $showAlert, presenting: selectedRobot) { robot in
            Button(role: .destructive) {
                withAnimation {
                    model.remove(robot: robot) 
                }
            } label: { Text("Remove") }
            
            Button(role: .cancel) {
                withAnimation {
                    deleting = false
                }
            } label: { Text("Cancel") }
            
        } message: { _ in
            Text("Are you sure you want to remove this card?")
        }
     
于 2022-02-08T23:18:12.903 回答