【问题标题】:How do I resolve Picker selection via Proxy?如何通过代理解决选择器选择?
【发布时间】:2020-09-23 20:07:10
【问题描述】:

场景:我想处理选择事件,同时通过 Picker 获取所选项目。
这是参考introduction discussion to the Picker Proxy

这是我目前所拥有的。我无法让事件处理程序触发/激活/运行doSomething()

import SwiftUI

struct ContentView: View {
    var body: some View {
        GeometryReader { _ in
            VStack {
                Text("PickerView")
                    .font(.headline)
                    .foregroundColor(.gray)
                    .padding(.top, 10)

                Picker("test", selection: Binding(get: { "" }, set: { _ in

                    doSomething()

                })) {
                    Text("Hello").id("1")
                    Text("Uncle").id("2")
                    Text("Ric").id("3")
                }.labelsHidden()

            }.background(RoundedRectangle(cornerRadius: 10)
                .foregroundColor(Color.white).shadow(radius: 1))
        }
        .padding()
    }

    func doSomething() {
        print("Hello Something!")
    }
}

注意: 我不知道如何处理get{},所以我在那里放了一个空字符串以满足编译器的要求。

我尝试评估闭包参数(通过print(*closure parameter*))但没有得到任何值,所以我放置了一个_ 占位符以满足编译器的要求。

我如何收获选择?

闭包参数在这里似乎不起作用;因此占位符。没有太多可以效仿的例子。

【问题讨论】:

    标签: binding swiftui picker


    【解决方案1】:

    这是一个可能的解决方案:

    struct ContentView: View {
        // extract picker values for easier access
        private let items = ["Hello", "Uncle", "Ric"]
    
        // store the currently selected value
        @State private var selection = 0
    
        // custom binding for the `selection`
        var binding: Binding<Int> {
            .init(get: {
                selection
            }, set: {
                selection = $0
                doSomething() // call another function after the `selection` is set
            })
        }
    
        var body: some View {
            Picker("test", selection: binding) {
                ForEach(0 ..< items.count) { index in // use `ForEach` to quickly generate picker values
                    Text(items[index])
                        .tag(index) // use `tag` instead of `id`
                }
            }
            .labelsHidden()
        }
    
        func doSomething() {
            print("Hello Something!")
        }
    }
    

    【讨论】:

    • 这正是我想要的。感谢您的快速回复!
    【解决方案2】:

    讨论:

    您可以使用 onChange 触发值更改的副作用,例如环境键或绑定...

    (苹果文档)

        struct ContentView: View {
        @State
        private var selection = 0
        let items = ["Hello", "Uncle", "Ric"]
        var body: some View {
            VStack {
                Picker("", selection: $selection) {
                    ForEach(0..<items.count) {
                        Text(items[$0])
                            .tag($0)
                    }
                }
                .onChange(of: selection) { _ in
                    print("Hello Something!")
                }
            }
        }
     }
    

    【讨论】:

      猜你喜欢
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      相关资源
      最近更新 更多