wilshipley
(Wil Shipley)
February 8, 2020, 12:59am
21
Fascinatingly, the above code works perfectly if I use the iOS-style toggle (on macOS):
Toggle(isOn: $usePaperLook) { Text("Use Paper Texture") }
.toggleStyle(CheckboxToggleStyle())
But it doesn’t work for TextField
either — same problem, the default gets read and written but the user input doesn’t draw after hitting ‘return’.
As another datapoint this questioner on StackOverflow had the same kind of problem that I’m seeing — the widget kept reverting its state to its original state .
young
(rtSwift)
May 4, 2020, 7:00pm
22
Came across this: "My NSUserDefaults wrapper called Defaults now includes a SwiftUI property wrapper that is a drop-in replacement for @<200b>State
. It will trigger view updates when the user defaults item changes."
1 Like
young
(rtSwift)
June 26, 2020, 10:17pm
23
@AppStorage takes care of the need now
3 Likes
swiftyui
(LW)
August 30, 2020, 10:03pm
24
All above way will make @State absent from reactive. Here is an example
@propertyWrapper
struct UserDefault {
let key: String
let defaultValue: Value
init(wrappedValue value: Value, key: String) {
self.defaultValue = value
self.key = key
}
var wrappedValue: Value {
get { UserDefaults.standard.object(forKey: key) as? Value ?? defaultValue }
nonmutating set { UserDefaults.standard.set(newValue, forKey: key) }
}
var projectedValue: Binding<Value> {
Binding<Value>( get: { self.wrappedValue }, set: { newValue in self.wrappedValue = newValue } )
}
}
struct TogetherWithStateDemo: View {
@State @UserDefault (key: "booletstKey") var toogleValue: Bool = true
@State @UserDefault (key: "stringTestKey") var stringValue: String = "Default Value"
public var body: some View {
VStack {
Toggle(isOn: $toogleValue[dynamicMember: \.wrappedValue]) {
Text("Use Paper Texture")
}
Text(stringValue)
Button("Change string value") {
stringValue = "Changed string value"
}
}
}
}
Now, if you click the button, the text is not updated any more