从 UISegmentedControl 获取字符串值

如何在 UISegmentedControl 中获取段的文本值?

57913 次浏览

Objective-C

NSString *title = [segment titleForSegmentAtIndex:segment.selectedSegmentIndex];

Swift:

let title = segment.titleForSegment(at: segment.selectedSegmentIndex)
[segmentedControl titleForSegmentAtIndex:int];

For the current selected index

[segmentedControl titleForSegmentAtIndex:[segmentedControl selectedSegmentIndex]];

As this is the first Google result and we are now in the swift era:

Swift:

seg_ctrl.titleForSegmentAtIndex( seg_ctrl.selectedSegmentIndex)

Swift 3:

segmentedControl.titleForSegment(at: segmentedControl.selectedSegmentIndex)