SwiftUI 上下文菜单没有传递正确的变量

SwiftUI context menu not passing correct variable

我希望显示的 sheet 显示列表中按下的实际字符串。

即长按 G 并且显示 sheet 应该显示 G

不幸的是它没有,我假设这是一个 SwiftUI 错误。

有人对此有干净的解决方案吗?

谢谢

struct ContentView: View {

    let items = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

    @State var showing = false

    var body: some View {
        NavigationView {
            List {
                ForEach(items, id: \.self) { item in
                    Text(item).font(.largeTitle)
                        .contextMenu {
                            self.contextEdit(item)
                    }
                }
            }
        }
    }

    private func contextEdit(_ item: String) -> some View {
        Button(action: {
            self.showing.toggle()
            print(item)
        }) {
            Text("Edit")
            Image(systemName: "circle")
        }.sheet(isPresented: $showing) {
            Text(item)
        }
    }
}

sheets 只应在顶层使用。这会导致意外行为,因为输出中的警告也应该说。

这是一个工作示例:

struct ContentView: View {

    let items = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

    @State var showing = false
    @State var currentItem: String = ""

    var body: some View {
        NavigationView {
            List {
                ForEach(items, id: \.self) { item in
                    Text(item).font(.largeTitle)
                        .contextMenu {
                        Button(action: {
                            self.currentItem = item
                            self.showing.toggle()
                        }) {
                            Text("Edit")
                            Image(systemName: "circle")
                        }
                    }
                }
            }
        }.sheet(isPresented: self.$showing) {
            Text(self.currentItem)
        }
    }
}

希望对您有所帮助

你好 krjw

经过大量的试验和错误后,我发现他的作品很适合我的需要

struct PresentingContextItem<Destination: View>: View {

    @State private var showingDestination: Bool = false
    var destination: () -> Destination
    let title: String
    let image: String

    init(title: String, image: String, @ViewBuilder _ destination: @escaping () -> Destination) {
        self.destination = destination
        self.title = title
        self.image = image
    }

    var body: some View {
        Button(action: {
            self.showingDestination.toggle()
        }) {
            Text(self.title)
            Image(systemName: self.image)
        }.sheet(isPresented: self.$showingDestination) {
            self.destination()
        }
    }
}

用法

struct ContentView: View {

    let items = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

    var body: some View {
        NavigationView {
            List(items, id: \.self) { item in
                Text(item).font(.largeTitle)
                    .contextMenu {
                        // option 1
                        PresentingContextItem(title: "Create", image: "circle") {
                            Text("Edit...\(item)").foregroundColor(.red)
                        }
                        // option 2
                        self.starItem(item)
                }
            }
        }
    }

    private func starItem(_ item: String) -> some View {
        PresentingContextItem(title: "Star", image: "star") {
            Text("Star...\(item)").foregroundColor(.green)
        }
    }
}