I have a Picker
in a SwiftUI Form
, and I'm trying to process the value whenever the picker changes. I expected to be able to do this with didSet
on a variable that represents the currently selected value.
import SwiftUI
struct ContentView: View {
enum TransmissionType: Int {
case automatic
case manual
}
@State private var selectedTransmissionType: Int = TransmissionType.automatic.rawValue {
didSet {
print("selection changed to \(selectedTransmissionType)")
}
}
private let transmissionTypes: [String] = ["Automatic", "Manual"]
var body: some View {
NavigationView {
Form {
Section {
Picker(selection: $selectedTransmissionType,
label: Text("Transmission Type")) {
ForEach(0 ..< transmissionTypes.count) {
Text(self.transmissionTypes[$0])
}
}
}
}
}
}
}
The Picker
UI works (mostly) as expected: I can see the default value is selected, tap into the picker, it opens a new view, I can select the other value, and then it goes back to the main form and shows me the new value is selected. However, didSet
is never called.
I saw this question, but it seems odd to me to add more to my View
code instead of just processing the new value when the variable changes, if that is even possible. Is it better to use onReceive
even though it results in a more complicated view? My main question is: What's wrong with my code to prevent didSet
from being called?
I used this example to get to this point.
Beyond my regular question, I have a few others about this example:
A) It seems weird how I have an enum
and also an Array
to represent the same two values. Can someone also suggest a better way to structure it to avoid this redundancy? I considered a TransmissionType
object, but that seemed like overkill compared to an enum
... maybe it's not?
B) When tapping into the picker, the screen with the picker options slides over, and then the two options jump up a bit. This feels jarring and jumpy and a bad user experience. Am I doing something wrong here that's causing the bad UX? Or is it probably a SwiftUI bug? I'm getting this error every time I change the picker:
[TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window.