Post

Replies

Boosts

Views

Activity

SwiftUI - Picker Binding not Working :-(
I am trying to bind a Picker, but it's not working. I create a BindableObject, an instance of Settings, add it to the environment using environmentObject() in SceneDelegate, and address it in the View using @EnvironmentObjectstruct ContentView : View { var favoriteFoods = ["Tofu", "Seitan", "Nilla Wafers", "Avocado Toast"] @EnvironmentObject var settings : Settings // meanwhile, inside var body: some View ... Picker(selection: $settings.favoriteFoodChoice, label: Text("Favorite Food")){ ForEach(self.favoriteFoods.identified(by: \.self)){ food in Text(food) } }Here's what my Settings looks like:class Settings : BindableObject { var didChange = PassthroughSubject<void,never>() var favoriteFoodChoice:Int { willSet { print("Favorite Food Choice will be \(newValue)") didChange.send() print("Favorite Food Choice: \(favoriteFoodChoice)")// never changes when I select a different food. } didSet { print("Favorite Food Choice was \(oldValue)") didChange.send() print("Favorite Food Choice: \(favoriteFoodChoice)")// never changes when I select a different food. } } init(favoriteFoodChoice:Int) { self.favoriteFoodChoice = favoriteFoodChoice } convenience init(){ self.init(favoriteFoodChoice:0) } }When I change the selected food, I see output from print(), but favoriteFoodChoice stays the same. Isn't it supposed to change?? Or am I misunderstanding how binding works with Pickers?Any help would be appreciated!
7
0
11k
Jul ’22
SwiftUI - Picker Binding not Working :-(
I am trying to bind a Picker, but it's not working. I create a BindableObject, an instance of Settings, add it to the environment using environmentObject() in SceneDelegate, and address it in the View using @EnvironmentObjectstruct ContentView : View { var favoriteFoods = ["Tofu", "Seitan", "Nilla Wafers", "Avocado Toast"] @EnvironmentObject var settings : Settings // meanwhile, inside var body: some View ... Picker(selection: $settings.favoriteFoodChoice, label: Text("Favorite Food")){ ForEach(self.favoriteFoods.identified(by: \.self)){ food in Text(food) } }Here's what my Settings looks like:class Settings : BindableObject { var didChange = PassthroughSubject<void,never>() var favoriteFoodChoice:Int { willSet { print("Favorite Food Choice will be \(newValue)") didChange.send() print("Favorite Food Choice: \(favoriteFoodChoice)")// never changes when I select a different food. } didSet { print("Favorite Food Choice was \(oldValue)") didChange.send() print("Favorite Food Choice: \(favoriteFoodChoice)")// never changes when I select a different food. } } init(favoriteFoodChoice:Int) { self.favoriteFoodChoice = favoriteFoodChoice } convenience init(){ self.init(favoriteFoodChoice:0) } }When I change the selected food, I see output from print(), but favoriteFoodChoice stays the same. Isn't it supposed to change?? Or am I misunderstanding how binding works with Pickers?Any help would be appreciated!
Replies
7
Boosts
0
Views
11k
Activity
Jul ’22