<!--
{
  "availability" : [
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: 14.0.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UISegmentedControl/setAction(_:forSegmentAt:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UISegmentedControl(im)setAction:forSegmentAtIndex:"
  },
  "title" : "setAction(_:forSegmentAt:)"
}
-->

# setAction(_:forSegmentAt:)

Sets the action for the segment at the index you specify.

```
func setAction(_ action: UIAction, forSegmentAt segment: Int)
```

## Parameters

`action`

A [`UIAction`](/documentation/UIKit/UIAction) object to set on the segment at the index you specify.

`segment`

An integer index of a segment.

## Discussion

Segments prefer images over titles when the action contains both. Selecting a segment invokes the action’s [`UIActionHandler`](/documentation/UIKit/UIActionHandler), as well as handlers for the [`valueChanged`](/documentation/UIKit/UIControl/Event/valueChanged) and [`primaryActionTriggered`](/documentation/UIKit/UIControl/Event/primaryActionTriggered) control events.

> Note:
> This method asserts an error if the action’s ``doc://com.apple.uikit/documentation/UIKit/UIAction/Identifier-swift.struct`` doesn’t match the action of the existing segment at this index, or isn’t unique within all actions associated with the segmented control.

---

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)