<!--
{
  "availability" : [
    "macOS: 10.15.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/onCommand(_:perform:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE9onCommand_7performQr10ObjectiveC8SelectorV_yycSgtF"
  },
  "title" : "onCommand(_:perform:)"
}
-->

# onCommand(_:perform:)

Adds an action to perform in response to the given selector.

```
nonisolated func onCommand(_ selector: Selector, perform action: (() -> Void)?) -> some View
```

## Parameters

`selector`

The selector to register for `action`.

`action`

The action to perform. If `action` is `nil`, `command`
keeps its association with this view but doesn’t trigger.

## Return Value

A view that triggers `action` when the `command` occurs.

## Discussion

This view or one of the views it contains must be in focus in order
for the action to trigger. Other actions for the same command
on views *closer* to the view in focus take priority, potentially
overriding this action.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)