7

I have placed an UISegmentedControl into my XIB file. Basically, when the the second tab of the control is tapped (aka segment 1, the first segment is segment 0), I want to unhide a text field. I know how to unhide the text field, but how do I detect which part of the segmented control the user has tapped?

[textField setHidden:NO];
Jack Humphries
  • 13,056
  • 14
  • 84
  • 125

3 Answers3

23

Create an IBAction like the one below and connect it to the valueChanged action in Interface Builder.

- (IBAction)segmentedControlChanged:(id)sender
{
   UISegmentedControl *s = (UISegmentedControl *)sender;

   if (s.selectedSegmentIndex == 1)
   {
      [countTextField setHidden:NO];
   }
}
Brandon Schlenker
  • 5,078
  • 1
  • 36
  • 58
11

You should hook up your segmeted controls valueChanged action in IB to a method in your view controller that checks your segmented controls selectedSegmentIndex.

thelaws
  • 7,991
  • 6
  • 35
  • 54
3

You should add a target-action for the control event UIControlEventValueChanged and check the selectedSegmentIndex of the segmented control.

If it is 1 then hide the text field.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105