13

I have a UISegmentedControl button with three segments. In ViewController.m this is working just fine -- pressing the buttons fires the correct methods.

I have another separate UIButton that when it is pressed it needs to first CHECK the state of the UISegmentedControl (to see which button is currently pressed) and then fire a method according to that segment value.

Here is my code for that separate UIButton. The button itself is working, but I cannot seem to figure out how to GET the current value of the segment of the UISegmentedControl.

Many thanks for any assistance here. I am new to OBJ-C. I know how to do this in VisualBasic, so answers that are on the more verbose side would be most appreciated as I need to know the 'why'. Thank you.

- (IBAction)decodeButton:(id)sender {
    UISegmentedControl *segment = [UISegmentedControl alloc];  // THIS DOES NOT WORK.

    if (segment.selectedSegmentIndex == 0) {
                decode(textToDecode);
    } else if(segment.selectedSegmentIndex == 1) {
                decode1(textToDecode);
    } else if(segment.selectedSegmentIndex == 2) {
                decode2(textToDecode); 
    }
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
mstace
  • 177
  • 1
  • 1
  • 8

5 Answers5

19

Here is a Tutorial using UISegmentedControl in iOS.

Just Create a Reference object and wire it properly to File Owner.

IBOutlet UISegmentedControl *segmentedControl;

Then set property

@property (strong, nonatomic) IBOutlet UISegmentedControl * segmentedControl;

Synthesize in .m file

@synthesize segmentedControl;

Now You can Access the selected index at any time.

- (IBAction)decodeButton:(id)sender {

    if (segmentedControl.selectedSegmentIndex == 0) {
                decode(textToDecode);
    } else if(segmentedControl.selectedSegmentIndex == 1) {
                decode1(textToDecode);
    } else if(segmentedControl.selectedSegmentIndex == 2) {
                decode2(textToDecode); 
    }
}
Siba Prasad Hota
  • 4,779
  • 1
  • 20
  • 40
  • Thank you, @Siba. The Tutorial was exactly what I needed to see. This code worked perfectly and the tutorial gave me a label to show -- which was my next task. 2013 is already starting out very well. I thank all of you who responded. – mstace Dec 31 '12 at 20:01
6

Your code alloc every time UISegmentedControl in the button press action. So use the following code for sUISegmentedControl creation and its action .

 SegmentChangeView=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Segment1",@"Segment2",@"Segment3",nil]];
    SegmentChangeView.frame=CGRectMake(5, 44, self.view.bounds.size.width-10, 33);
    SegmentChangeView.selectedSegmentIndex=0;
    SegmentChangeView.segmentedControlStyle=UISegmentedControlStyleBar;
    SegmentChangeView.momentary = YES;
    [SegmentChangeView setTintColor:[UIColor blackColor]];
    NSDictionary *attributes =[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:13],UITextAttributeFont,nil];
    [SegmentChangeView setTitleTextAttributes:attributes forState:UIControlStateNormal];
    [SegmentChangeView addTarget:self action:@selector(SegmentChangeViewValueChanged:) forControlEvents:UIControlEventValueChanged];
    SegmentChangeView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleBottomMargin;
    [self.view addSubview:SegmentChangeView];

-(IBAction)SegmentChangeViewValueChanged:(UISegmentedControl *)SControl
{
    if (SControl.selectedSegmentIndex==0)
    {
          decode(textToDecode);
    }
    else if (SControl.selectedSegmentIndex==1)
    {
            decode1(textToDecode);
    }
else if (SControl.selectedSegmentIndex==2)
    {
            decode2(textToDecode);
    }


}
Senthilkumar
  • 2,471
  • 4
  • 30
  • 50
3

You should remove UISegmentedControl *segment = [UISegmentedControl alloc] ; from your code, as it allocs anew instance of your UISegmentedControl every time, instead,

create an outlet for you UISegmentController like

@property (strong, nonatomic) IBOutlet UISegmentedControl * segment;

and then later at any point in your viewcontroller.m file, you can get the currently selected segment by using

segment.selectedSegmentIndex;

Hope this make sense,

Regards

DD_
  • 7,230
  • 11
  • 38
  • 59
2

Try like this

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

   if (s.selectedSegmentIndex == 1)
   {
      //code
   }
   else
   {
      //code
   }
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
1

This code means you are creating a new Object on every click

 UISegmentedControl *segment = [UISegmentedControl alloc] ;

The thing you have to do take IBOutlet (Property) of your segmentedControl then I will work for you. dont create a new object in the button method. when you will make a IBOutlet it will be link with at segmentControl and your code will work that time . Thanks

DD_
  • 7,230
  • 11
  • 38
  • 59
junaidsidhu
  • 3,539
  • 1
  • 27
  • 49