Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Last active July 18, 2020 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/d95dca55f6c32c50bdce45ab53301af8 to your computer and use it in GitHub Desktop.
Save sturdysturge/d95dca55f6c32c50bdce45ab53301af8 to your computer and use it in GitHub Desktop.
RevDoc FocusedView
struct ContentView: View {
var body: some View {
VStack {
TextFieldView()
DisplayTextView()
}
.padding(20)
}
}
struct TextFieldView: View {
@State var text = ""
var body: some View {
TextField("", text: $text)
.focusedValue(\.text, $text)
}
}
struct DisplayTextView: View {
@FocusedBinding(\.text) var text: String?
var body: some View {
Text(text ?? "Blank")
}
}
struct FocusedTextKey : FocusedValueKey {
typealias Value = Binding<String>
}
extension FocusedValues {
var text: FocusedTextKey.Value? {
get { self[FocusedTextKey.self] }
set { self[FocusedTextKey.self] = newValue }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment