SwiftUI views don't really have concepts of lifetime. So it's hard to say when a view is recreated and needs to reload the initial values.
If your reload events coincide with the view appearances though, you can just use onAppear
.
SwiftUI views don't really have concepts of lifetime. So it's hard to say when a view is recreated and needs to reload the initial values.
If your reload events coincide with the view appearances though, you can just use onAppear
.
You can override @State value by giving the view a different id():
import SwiftUI
/// You can override @State value as long as you give it a new id!!
struct FooFooPicker: View {
@State private var selection: Int
init(_ val: Int) {
_selection = State(wrappedValue: val)
}
var body: some View {
VStack {
Text("Selection: \(selection)")
Picker("Numbers", selection: $selection) {
ForEach(0...5, id: \.self) { number in
Text(verbatim: "\(number)").tag(number)
}
}
}
}
}
struct PickerDefaultSettable: View {
@State var defaultOverride = 0
@State var id = 0
var body: some View {
VStack {
FooFooPicker(defaultOverride)
.id(id) // invalidate the view state
Text("DefaultOverride: \(defaultOverride)")
VStack {
ForEach(0...5, id: \.self) { number in
Button("\(number)") {
defaultOverride = number
id += 1 // generate a new id
}
}
}
}
}
}
struct PickerDefaultSettable_Previews: PreviewProvider {
static var previews: some View {
PickerDefaultSettable()
}
}