Simple Binding example
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
struct NameDisplayView: View { | |
@State var name: String = "unknown" | |
@State var showNameChange = false | |
var body: some View { | |
VStack { | |
Text("Your name is \(name)") | |
Divider() | |
Button("Change") { | |
self.showNameChange = true | |
} | |
} | |
.sheet(isPresented: $showNameChange) { | |
NameChangeView(text: self.$name) | |
} | |
} | |
} | |
struct NameChangeView: View { | |
@Environment(\.presentationMode) var presentationMode | |
@Binding var text: String | |
var body: some View { | |
TextField("Type Here", text: $text, onCommit: { self.presentationMode.wrappedValue.dismiss() }) | |
.textFieldStyle(RoundedBorderTextFieldStyle()) | |
.padding() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment