---
title: "setAction(_:forSegmentAt:)"
framework: uikit
role: symbol
role_heading: Instance Method
path: "uikit/uisegmentedcontrol/setaction(_:forsegmentat:)"
---

# setAction(_:forSegmentAt:)

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

## Declaration

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

## Parameters

- `action`: A doc://com.apple.uikit/documentation/UIKit/UIAction object to set on the segment at the index you specify.
- `segment`: An integer index of a segment.

## Discussion

Discussion Segments prefer images over titles when the action contains both. Selecting a segment invokes the action’s UIActionHandler, as well as handlers for the valueChanged and primaryActionTriggered control events. note: This method asserts an error if the action’s UIAction.Identifier doesn’t match the action of the existing segment at this index, or isn’t unique within all actions associated with the segmented control.

## See Also

### Managing segment actions

- [actionForSegment(at:)](uikit/uisegmentedcontrol/actionforsegment(at:).md)
