-
-
Save sturdysturge/d95dca55f6c32c50bdce45ab53301af8 to your computer and use it in GitHub Desktop.
RevDoc FocusedView
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 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