Created
October 10, 2021 18:58
-
-
Save 0xWDG/52a78f37d1475a2239ff704176e5742a to your computer and use it in GitHub Desktop.
SwiftUI Binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// SwiftUITest | |
// | |
import SwiftUI | |
class ClassWithVariables { | |
var intVar: Int | |
var strVar: String | |
var booVar: Bool | |
public static let shared = ClassWithVariables() | |
init() { | |
intVar = 1 | |
strVar = "Hello" | |
booVar = true | |
} | |
} | |
struct ContentView: View { | |
var cls = ClassWithVariables.shared | |
// Placeholder. | |
@State private var updText = Binding( | |
get: { "" }, | |
set: { _ in } | |
) | |
// For alert view controller. | |
@State private var displayFromMemory = false | |
// could be directly, if we dont use `cls` | |
func afterInit() { | |
updText = Binding.init( | |
get: { | |
self.cls.strVar | |
}, | |
set: { newValue in | |
// This will be called more times | |
// Should not be a problem. | |
// This is an example log. | |
// the last 2 updates, | |
// removes the last character.. | |
// | |
// NewVal="Hello,doestiswork" | |
// NewVal="Hello,doestiswork" | |
// NewVal="Hello,doestiswor" | |
// NewVal="Hello,doestiswor" | |
print("NewVal=\"\(newValue)\"") | |
self.cls.strVar = newValue | |
} | |
) | |
} | |
var body: some View { | |
VStack { | |
Text( | |
"Hello, world!" | |
).padding() | |
TextField( | |
"Enter Test", | |
text: updText // <- will never updated? | |
) | |
.multilineTextAlignment(.center) | |
.border(.blue, width: 1) | |
.padding() | |
Button("Load text from memory") { | |
displayFromMemory = true | |
} | |
.alert(isPresented: $displayFromMemory) { | |
Alert( | |
title: Text("TEXT"), | |
message: Text("\"\(cls.strVar)\""), | |
dismissButton: .default( | |
Text("OK") | |
) | |
) | |
} | |
.onAppear( | |
perform: afterInit | |
) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// SwiftUITest | |
// | |
import SwiftUI | |
class ClassWithVariables { | |
var intVar: Int | |
var strVar: String | |
var booVar: Bool | |
public static let shared = ClassWithVariables() | |
init() { | |
intVar = 1 | |
strVar = "Hello" | |
booVar = true | |
} | |
} | |
struct ContentView: View { | |
@State private var updText = Binding.init( | |
get: { | |
ClassWithVariables.shared.strVar | |
}, | |
set: { newValue in | |
// This will be called more times | |
// Should not be a problem. | |
// This is an example log. | |
// the last 2 updates, | |
// removes the last character.. | |
// | |
// NewVal="Hello,doestiswork" | |
// NewVal="Hello,doestiswork" | |
// NewVal="Hello,doestiswor" | |
// NewVal="Hello,doestiswor" | |
print("NewVal=\"\(newValue)\"") | |
ClassWithVariables.shared.strVar = newValue | |
} | |
) | |
// For alert view controller. | |
@State private var displayFromMemory = false | |
var body: some View { | |
VStack { | |
Text( | |
"Hello, world!" | |
).padding() | |
TextField( | |
"Enter Test", | |
text: updText // <- will never updated? | |
) | |
.multilineTextAlignment(.center) | |
.border(.blue, width: 1) | |
.padding() | |
Button("Load text from memory") { | |
displayFromMemory = true | |
} | |
.alert(isPresented: $displayFromMemory) { | |
Alert( | |
title: Text("TEXT"), | |
message: Text("\"\(ClassWithVariables.shared.strVar)\""), | |
dismissButton: .default( | |
Text("OK") | |
) | |
) | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Author
0xWDG
commented
Oct 10, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment