2009-07-27 58 views
2

我需要跟蹤以前選擇的UISegmentControl段。是否有我可以使用的委託方法?也許像selectedSegmentShouldChange:?我所能找到的唯一代表方法是segmentedControl:selectedSegmentChanged:。這個代表是我需要的那一步之後的一步。存儲以前選擇的UISegmentedControl段?

回答

2

沒有處理這種情況的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