【问题标题】:SwiftUI context menu not passing correct variableSwiftUI 上下文菜单未传递正确的变量
【发布时间】:2020-03-08 02:06:44
【问题描述】:

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

即长按 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)
        }
    }
}

【问题讨论】:

  • 对我来说听起来像是 Apple Bug... :(

标签: contextmenu swiftui


【解决方案1】:

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

【讨论】:

  • 我有多个工作表供我查看 - 这就是我将工作表附加到按钮的原因。如果我将工作表附加到按钮,在您的示例中它仍然有效。我想目前我将不得不忍受我庞大的嵌套 ForEach。谢谢
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多