get string value from UISegmentedControl
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UISegmentedControl provides the selected segment's index via its selectedSegmentIndex property, not the title string directly. To get the string value, call titleForSegment(at:) with the selected index. This returns an Optional<String> because the segment might use an image instead of a title, or the index could be UISegmentedControl.noSegment (-1) when nothing is selected.
Getting the Selected Segment Title
Always unwrap safely since the result is optional:
Responding to Selection Changes
Use a target-action or @IBAction to respond when the user taps a segment:
With Interface Builder:
Mapping Segments to Enum Values
For type-safe handling, map segment indices to an enum:
Getting All Segment Titles
Setting and Modifying Segment Titles
Handling No Selection
UISegmentedControl.noSegment equals -1. Passing -1 to titleForSegment(at:) returns nil.
SwiftUI Equivalent
In SwiftUI, use Picker with SegmentedPickerStyle:
SwiftUI binds the selected value directly to a String variable — no index-to-title conversion needed.
Common Pitfalls
- Not handling
noSegment(-1): If no segment is selected,selectedSegmentIndexreturns-1. Passing this totitleForSegment(at:)returnsnil. Always check forUISegmentedControl.noSegmentbefore accessing the title. - Force-unwrapping the optional title:
titleForSegment(at:)returnsString?. Segments can use images instead of titles, sonilis a valid return. Force-unwrapping (title!) crashes if the segment has an image instead of a title. - Using hard-coded index-to-string mapping:
if index == 0 { return "Daily" }breaks when segments are reordered or titles change. UsetitleForSegment(at:)or an enum mapped to indices for maintainable code. - Forgetting to set an initial selection: If
selectedSegmentIndexis not set, it defaults to-1(no selection) unless set in Interface Builder. Users see no highlighted segment, and reading the title returnsnil. - Not connecting the
valueChangedevent:UISegmentedControlfires.valueChangedwhen the user taps a different segment. If you connect to.touchUpInsideinstead, the action never fires.
Summary
- Use
titleForSegment(at: selectedSegmentIndex)to get the selected segment's string value - The return type is
String?— always unwrap safely withif letorguard let - Connect to the
.valueChangedcontrol event to respond to segment changes - Map indices to an enum for type-safe segment handling
selectedSegmentIndexreturns-1(noSegment) when nothing is selected- In SwiftUI, use
Pickerwith.segmentedstyle for direct string binding
Related reading
- Get the current displaying UIViewController on the screen in AppDelegate.m
- Get the current language in device
- Get to UIViewController from UIView?
- Get top most UIViewController
- Get UIScrollView to scroll to the top
- Get Unix Epoch Time in Swift
- Get Value of a Edit Text field
- Get visible items in RecyclerView
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.