この状況を処理するためのAPIはありません。代わりに、最後に選択したセグメントを追跡するために単純なFIFOバッファを使用する必要がありました。ここに私のPreviousItem
オブジェクトのコードは次のとおりです。
// PreviousItem.h
#import <Foundation/Foundation.h>
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
それの例は、使用です:
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