Skip to content

Instantly share code, notes, and snippets.

@JoshHrach
Last active April 28, 2020 23:56
Show Gist options
  • Save JoshHrach/990b784b661de63485670cfda32a9e10 to your computer and use it in GitHub Desktop.
Save JoshHrach/990b784b661de63485670cfda32a9e10 to your computer and use it in GitHub Desktop.
Simple Binding example
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