Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISegmentedControl get previous selected index

I have a UISegmentedControl with a segmentedValueChanged method on it. I need to find out what was the selected index before this method is called . How to do this?

like image 388
user1028028 Avatar asked Oct 30 '13 10:10

user1028028


2 Answers

Use this function:

@property (nonatomic, assign) NSInteger oldSegmentedIndex;
@property (nonatomic, assign) NSInteger actualSegmentedIndex;
.......

- (void)viewDidLoad
{
    .......
    self.oldSegmentedIndex = -1;
    self.actualSegmentedIndex = self.segmentedControl.selectedSegmentIndex;
    .......
}
.......
- (IBAction)didSegmentedValueChanged:(id)sender
{
    //Do whatever you want
    self.oldSegmentedIndex = self.actualSegmentedIndex;
    self.actualSegmentedIndex = self.segmentedControl.selectedSegmentIndex;
}
like image 124
Alex Avatar answered Nov 16 '22 01:11

Alex


The best way is to keep a variable that holds the last selected index. Then you just check it's value when you need

like image 32
Zalykr Avatar answered Nov 16 '22 03:11

Zalykr