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 - Debug Preview not working in Xcode 12 beta 2?
Nothing prints to the console when I tap on a button, although the debugger will break on the print() lines. I've chosen Debug Preview rather than just Live Preview in the Canvas, and I've tried restarting Xcode. Before I file feedback, is anyone else seeing this?     var body: some View {         VStack(alignment: .leading){             Button("One Fish"){                 print("One")             }             Button("Two fish"){                 print("Two")             }         }     } }
22
0
8.6k
Sep ’21
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
SwiftUI - Debug Preview not working in Xcode 12 beta 2?
Nothing prints to the console when I tap on a button, although the debugger will break on the print() lines. I've chosen Debug Preview rather than just Live Preview in the Canvas, and I've tried restarting Xcode. Before I file feedback, is anyone else seeing this?     var body: some View {         VStack(alignment: .leading){             Button("One Fish"){                 print("One")             }             Button("Two fish"){                 print("Two")             }         }     } }
Replies
22
Boosts
0
Views
8.6k
Activity
Sep ’21