views:

278

answers:

2

I need to keep track of the previously selected segment of a UISegmentControl. Is there a delegate method I could use? Maybe something like selectedSegmentShouldChange:? The only delegate method I have been able to find is segmentedControl:selectedSegmentChanged:. This delegate is one step after the one I need.

A: 

I'd love to know the answer to this question.

yodie
+1  A: 

There is not an API for handling this situation. I instead had to work with a simple FIFO buffer to keep track of the last selected segment. Here is the code for my PreviousItem object:

// PreviousItem.h

#import 

typedef struct {
    char current;
    int a;
    int b;
} itemFIFO;

@interface PreviousItem NSObject {
    itemFIFO stack;
}

- (void) push(int) a;
- (int) pop;

@end

// PreviousItem.m

#import "PreviousItem.h"


@implementation PreviousItem

- (id) init
{
    if ( ![super init] ) {
        return nil;
    }

    stack.a = -1;
    stack.b = -1;

    return self;
}

- (void) push(int) a
{       
    stack.b = stack.a;
    stack.a = a;
}

- (int) pop
{
    return stack.b;
}

@end

An example of it's usage:

prevSegment = [[PreviousItem alloc] init];
[prevSegment push:0]; // Previously selected segment is 0
[mySegmentControl setSelectedSegment:1]; // Choose a new segment
[prevSegment push:1]; // Update our segment stack
// User does something and we need to know the previously selected segment
int oldSegment = [prevSegment pop]; // Will return 0 in this contrived example
jsumners