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

时间:2019-11-12 11:07:27

标签: contextmenu swiftui

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

,即长按G键,演示文稿应该显示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)
        }
    }
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

经过反复的尝试,我发现他的作品很适合我的需求

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)
        }
    }
}