I guess you may call it “conditional binding”. I don’t know how else to call it.
I want to change the title text, when either I type in the textfield or tap the button. However I don’t want to change the TextField text when I tap the button, just want to change the title text. This is the demo code
class Hobbit: ObservableObject {
@Published var firstName: String
init(name: String = "Bilbo") {
self.firstName = name
}
}
struct HobbitView: View {
@StateObject private var hobbit = Hobbit()
var body: some View {
//"Place Holder" should not be modified by tapping the button
TextField("Place Holder", text: $hobbit.firstName)
.padding(.bottom)
//Should be modifiable by TextField or Button
Text("The name is (hobbit.firstName).")
.font(.title)
//Should only modify the Text, not the TextField text
Button("Set a Name") {
hobbit.firstName = "Sam"
}
}
}
I’m new to SwiftUI and I think I’m missing some key knowledge on how SwiftUI bindings work.