Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISegmentedControl selected index always 0?

I have a UISegmentedControl that has 6 segments which I created in Interface Builder. I am calling the following method on value changed:

-(IBAction)segmentedChartButtonChanged:(id)sender
{
    switch (self.segmentedChartButton.selectedSegmentIndex) {
        case 0:
            NSLog(@"5d selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
            break;
        case 1:
            NSLog(@"3m selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
            break;
        default:
            break;
    }
}

Whenever I change the segments, the selectedSegmentIndex is always 0. Why is this?

like image 347
Sheehan Alam Avatar asked Jul 05 '11 16:07

Sheehan Alam


2 Answers

It is very likely that the IBOutlet isn't connected. Check that. You can also use the sender that is passed to the method.

UISegmentedControl * segmentedControl = (UISegmentedControl *)sender;
switch (segmentedControl.selectedSegmentIndex) {
    ....
like image 150
Deepak Danduprolu Avatar answered Sep 19 '22 14:09

Deepak Danduprolu


hi try like this it will work

-(IBAction)segmentedChartButtonChanged:(id)sender
 {
 UISegmentedControl *segment=(UISegmentedControl*)sender;
switch (segment.selectedSegmentIndex) {
    case 0:
        NSLog(@"5d selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
        break;
    case 1:
        NSLog(@"3m selected. Index: %d", self.segmentedChartButton.selectedSegmentIndex);
        break;
    default:
        break;
}

}

like image 20
ajay Avatar answered Sep 21 '22 14:09

ajay