UISegmentedControl text programmatically

I created a UISegmentedControl with two segments using the interface builder (from the storyboard), but I would like to program the text of the two segments. I want to do this because I use NSLocalizedString for all my buttons, shortcuts, titles, etc. I create all the materials in the interface builder, and then add the text programmatically. I managed to get each element to work this way, but I can't find a way to add text to my UISegmentedControl.

Is there any way to do this? I am trying to use the following, but since the segmented control is already created in the interface builder, it does not work.

[segmentedControl initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Title 1", @"Title 1"),NSLocalizedString(@"Title 2", @"Title 2"), nil]]; 

Thank you so much

+54
ios objective-c iphone ipad
Mar 22 2018-12-12T00:
source share
3 answers
 [segmentedControl setTitle:<YourLocalizedString> forSegmentAtIndex:0]; 
+171
Mar 22 '12 at 1:08
source share

Use setTitle: forSegmentAtIndex: to assign a title to your segments of a segmented control.

Hope this helps you.

+9
Mar 22 '12 at 1:08
source share

The correct answer for people using SWIFT 4 would be

 segmentedControl.setTitle("Your Title", forSegmentAt: 0) 
+2
Aug 03 '17 at 14:34 on
source share



All Articles