【发布时间】: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*))但没有得到任何值,所以我放置了一个_ 占位符以满足编译器的要求。
我如何收获选择?
闭包参数在这里似乎不起作用;因此占位符。没有太多可以效仿的例子。
【问题讨论】: