FocusedValues
struct · iOS 14.0+ · SDK 27.1
struct FocusedValuesA collection of state exported by the focused scene or view and its ancestors. ## Creating Custom Focused Values Use the ``Entry`` macro to create custom focused values by extending `FocusedValues` with new properties: extension FocusedValues { @Entry var focusedDocument: Binding<MyDocument>? } The ``Entry`` macro automatically generates the underlying key type and provides the getter and setter for the focused value. Since the default value for focused values is always `nil`, all focused values must be optional. ### Publishing Values in Your Views Views publish focused values using the ``View/focusedValue(_:_:)`` modifier: struct DocumentCellView: View { @Binding var document: MyDocument var body: some View { Text("Document Content") .focusedValue(\.focusedDocument, $document) } } For scene-wide values that should be available depending on the focused scene, use ``View/focusedSceneValue(_:_:)``: struct DocumentViewer: View { @Binding var document: MyDocument var body: some View { Text("Document Content") .focusedSceneValue(\.focusedDocument, $document) } } ### Accessing the current focused value Use the ``FocusedValue`` property wrapper in your ``App`` or ``View`` to read the current value in the `body`. The ``FocusedBinding`` can be used as a convenient way to access the `wrappedValue` if the value type of the focused value is a `Binding`: @main struct DocumentApp: App { @FocusedBinding(\.focusedDocument) var currentDocument: MyDocument? var body: some Scene { DocumentGroup(newDocument: MyDocument()) { file in ContentView(document: file.$document) .focusedValue(\.focusedDocument, file.$document) } .commands { CommandGroup(after: .undoRedo) { Button("Increment") { currentDocument?.value += 1 }.disabled(currentDocument == nil) } } } }
Members (0)
This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.
Skipped declarations
1 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.
FocusedValues.subscript(_:)
Passing a Swift type itself is not supported.