Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Last active Jul 18, 2020
Embed
What would you like to do?
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