0

Say I have a UITextField (default blank), a UISegmentedControl (default 2 segments named "1" & "2"), and a UILabel (default text "None") in my nib. I want to be able to do the following:

  1. I want to enter a number in the UITextField, say "3"
  2. I then want to redraw the UISegmentedControl to have 3 segments named "1", "2", and "3."
  3. I then want to select the segment in the UISegmentedControl, and reflect the selection in the UILabel. So if I select "3", UILabel will show "3"

I know how to set an IBAction that will catch the number entered in the UITextField, but how do #2 above, that is how do I "redraw" the UISegmentControl to have 3 segments named "1", "2", and "3?"

I know how to do #3.

Chris F
  • 14,337
  • 30
  • 94
  • 192

1 Answers1

0

You can do it by using the insertSegmentWithTitle:(NSString *) atIndex:(NSUInteger) animated:(BOOL) method.

Like:

int indexOfSegment = yourSegmentController.numberOfSegments;
[yourSegmentController insertSegmentWithTitle:yourTextfield.text atIndex:indexOfSegment animated:YES];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Thanks this worked. I [yourSegmentController removeAllSegments} first, then re-added all the indices just to make sure. – Chris F Nov 29 '14 at 05:35