I am trying to run a function after a user has selected a picker option.
The idea is that the user can set a default station, so I need to be able to send the selected value to a function so I can save it inside the core data module. How can I achieve this?
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")
}
}
}
3
Answers
Instead of using
onRecive
try usingonChange
.Your code:
Correction:
this will trigger every time $selectedFrameworkIndex is changed.
I would use a simple
.onChange(of:)
call on the picker. It will pick up on the change of the@State
var and allow you to act on it. Use it like this:yours:
add a variable that checks to see if selectedFrameworkIndex has changed.
change: