I am writing a program which uses a picker to update the current view. However, I can not get the .onChange() event to occur. I wrote a small example to test it and it is still not working. Can anyone see what my issue is in the code?
Here is my non-event example:
import SwiftUI
struct PickerOnChangeTestView: View
{
@State var selectedCourse: String = "Math"
var courses: [String] = ["Math","Biology","Civics","History","Art","Philosophy","Sociology"]
var body: some View {
VStack {
// This Text should update when selectedCourse changes
Text( selectedCourse )
HStack {
Text("Course: ")
Picker("Course", selection: $selectedCourse) {
ForEach(0..<courses.count) {
index in
Text(courses[index])
.tag(index)
}
.onChange(of: selectedCourse,
perform: {
_ in
print("On Change")
})
.pickerStyle(MenuPickerStyle())
}
}
.padding(.leading)
.frame(width: 400, height:20)
}
}
}
struct PickerOnChangeTestView_Previews: PreviewProvider {
static var previews: some View {
PickerOnChangeTestView()
}
}