---
title: "setSelected(_:forSegment:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nssegmentedcell/setselected(_:forsegment:)"
---

# setSelected(_:forSegment:)

Sets the selection state of the specified segment.

## Declaration

```swift
func setSelected(_ selected: Bool, forSegment segment: Int)
```

## Parameters

- `selected`: doc://com.apple.documentation/documentation/Swift/true if you want to select the segment; otherwise, doc://com.apple.documentation/documentation/Swift/false.
- `segment`: The index of the segment whose selection state you want to set. This method raises an exception (doc://com.apple.documentation/documentation/Foundation/NSExceptionName/rangeException) if the index is out of bounds.

## Discussion

Discussion If the control allows only a single selection, this method deselects any other selected segments. If the trackingMode property of the segmented cell is set to NSSegmentedControl.SwitchTracking.momentary, then attempting to set the selected state of the segment will have no effect.

## See Also

### Specifying the Selected Segment

- [selectSegment(withTag:)](appkit/nssegmentedcell/selectsegment(withtag:).md)
- [makeNextSegmentKey()](appkit/nssegmentedcell/makenextsegmentkey().md)
- [makePreviousSegmentKey()](appkit/nssegmentedcell/makeprevioussegmentkey().md)
- [selectedSegment](appkit/nssegmentedcell/selectedsegment.md)
- [isSelected(forSegment:)](appkit/nssegmentedcell/isselected(forsegment:).md)
