选择器上的选择器
Posted
技术标签:
【中文标题】选择器上的选择器【英文标题】:Picker on select 【发布时间】:2021-04-30 02:05:15 【问题描述】:所以我试图在用户选择选择器选项后运行一个函数。
这个想法是用户可以设置一个默认站,所以我需要能够将选择的值发送到一个函数,这样我就可以将它保存在核心数据模块中。
我想知道是否有人知道如何做到这一点。
import SwiftUI
struct SettingsView: View
var frameworks = ["DRN1", "DRN1 Hits", "DRN1 United", "DRN1 Life"]
@State private var selectedFrameworkIndex = 0
func doSomethingWith(value: String)
print(value)
var body: some View
NavigationView
Form
Section
Picker(selection: $selectedFrameworkIndex, label: Text("Favorite Station"))
ForEach(0 ..< frameworks.count)
Text(self.frameworks[$0])
.onReceive([self.frameworks].publisher.first()) value in
self.doSomethingWith(value: value)
.navigationBarTitle("Settings")
【问题讨论】:
这能回答你的问题吗? How can I run an action when a state changes? 如果你想向后兼容,使用.onReceive(Just(selectedFrameworkIndex))
而不是.onChange
【参考方案1】:
不要使用onRecive
,而是尝试使用onChange
。
您的代码:
.onReceive([self.frameworks].publisher.first()) value in
self.doSomethingWith(value: value)
更正:
.onChange(of: $selectedFrameworkIndex, perform: value in
self.doSomethingWith(value: value)
)
这将在每次更改 $selectedFrameworkIndex 时触发。
【讨论】:
【参考方案2】:我会在选择器上使用一个简单的.onChange(of:)
调用。它将了解@State
var 的变化并允许您对其采取行动。像这样使用它:
import SwiftUI
struct SettingsView: View
var frameworks = ["DRN1", "DRN1 Hits", "DRN1 United", "DRN1 Life"]
@State private var selectedFrameworkIndex = 0
func doSomethingWith(value: String)
print(value)
var body: some View
NavigationView
Form
Section
Picker(selection: $selectedFrameworkIndex, label: Text("Favorite Station"))
ForEach(0 ..< frameworks.count)
Text(self.frameworks[$0])
// Put your function call in here:
.onChange(of: selectedFrameworkIndex) value in
print(value)
.navigationBarTitle("Settings")
【讨论】:
唯一的问题是它只适用于 ios 14+,我们的用户向后兼容 iOS 13。【参考方案3】:你的:
import SwiftUI
struct SettingsView: View
var frameworks = ["DRN1", "DRN1 Hits", "DRN1 United", "DRN1 Life"]
@State private var selectedFrameworkIndex = 0
func doSomethingWith(value: String)
print(value)
var body: some View
NavigationView
Form
Section
Picker(selection: $selectedFrameworkIndex, label: Text("Favorite Station"))
ForEach(0 ..< frameworks.count)
Text(self.frameworks[$0])
.onReceive([self.frameworks].publisher.first()) value in
self.doSomethingWith(value: value)
.navigationBarTitle("Settings")
添加一个变量来检查 selectedFrameworkIndex 是否已更改。 改变:
import SwiftUI
struct SettingsView: View
var frameworks = ["DRN1", "DRN1 Hits", "DRN1 United", "DRN1 Life"]
@State private var selectedFrameworkIndex = 0
@State private var savedFrameworkIndex = 0
func handler<Value: Any>(val: Value) -> Value
if selectedFrameworkIndex != savedFrameworkIndex
self.doSomethingWith(value: self.frameworks[selectedFrameworkIndex])
return val
func doSomethingWith(value: String)
print(value)
var body: some View
NavigationView
Form
Section
Picker(selection: handler(val: $selectedFrameworkIndex), label: Text("Favorite Station"))
ForEach(0 ..< frameworks.count)
Text(self.frameworks[$0])
.navigationBarTitle("Settings")
【讨论】:
以上是关于选择器上的选择器的主要内容,如果未能解决你的问题,请参考以下文章
覆盖链式选择器上的 Tailwind CSS @apply 指令