I tired to build a multicomponent picker based on user3441734 solution for a dynamic picker. This picker lives in its own .swift file. I can’t figure out how to save the selection in a variable to access it from another view again.
Here is my code so far. I marked my wrong solution with an ???? and its error message below.
import SwiftUI
struct DynamicPicker: View {
@ObservedObject var model = Model()
// var to store the selection
@State var selection: String = ""
var body: some View {
VStack {
GeometryReader { geometry in
HStack {
Picker(selection: self.$model.selectedManufacturer, label: Text("")){
ForEach(0 ..< self.model.manufacturerNames.count){ index in
Text(self.model.manufacturerNames[index])
}
}.labelsHidden()
.frame(maxWidth: geometry.size.width * CGFloat(0.3333))
.clipped()
Picker(selection: self.$model.selectedStock, label: Text("")){
ForEach(0 ..< self.model.stockNamesCount){ index in
Text(self.model.stockNames[index])
}
}
.id(self.model.id)
.labelsHidden()
.frame(maxWidth: geometry.size.width * CGFloat(0.6666))
.clipped()
}
}
// Show selection
Text("\(self.model.manufacturerNames[model.selectedManufacturer])-\(self.model.stockNames[model.selectedStock])")
// Save selection to variable ????
selection = "\(self.model.manufacturerNames[model.selectedManufacturer])-\(self.model.stockNames[model.selectedStock])"
}
}
}
???? Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols