如何通过 Proxy 解决 Picker 选择问题?
How do I resolve Picker selection via Proxy?
场景: 我想在通过选择器获取所选项目的同时处理选择事件。
这是参考 .
这就是我目前所拥有的。我无法将事件处理程序获取到 fire/activate/run 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*)
)但没有得到任何值,所以我放置了一个 _
占位符以满足编译器的要求。
我如何收获选择?
闭包参数在这里似乎不起作用;因此占位符。没有多少例子可以遵循。
这是一个可能的解决方案:
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 = [=10=]
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!")
}
}
Discussion:
You can use onChange to trigger a side effect as the result of a value changing, such as an Environment key or a Binding...
(Apple doc.)
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[[=10=]])
.tag([=10=])
}
}
.onChange(of: selection) { _ in
print("Hello Something!")
}
}
}
}
场景: 我想在通过选择器获取所选项目的同时处理选择事件。
这是参考
这就是我目前所拥有的。我无法将事件处理程序获取到 fire/activate/run 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*)
)但没有得到任何值,所以我放置了一个 _
占位符以满足编译器的要求。
我如何收获选择?
闭包参数在这里似乎不起作用;因此占位符。没有多少例子可以遵循。
这是一个可能的解决方案:
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 = [=10=]
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!")
}
}
Discussion:
You can use onChange to trigger a side effect as the result of a value changing, such as an Environment key or a Binding...
(Apple doc.)
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[[=10=]])
.tag([=10=])
}
}
.onChange(of: selection) { _ in
print("Hello Something!")
}
}
}
}