2009-07-27 10 views
2

J'ai besoin de suivre le segment précédemment sélectionné d'un UISegmentControl. Y a-t-il une méthode de délégué que je pourrais utiliser? Peut-être quelque chose comme selectedSegmentShouldChange:? La seule méthode de délégué que j'ai pu trouver est segmentedControl:selectedSegmentChanged:. Ce délégué est une étape après celle dont j'ai besoin.Stocker le segment précédemment sélectionné d'un UISegmentedControl?

Répondre

2

Il n'existe aucune API pour gérer cette situation. Je devais plutôt travailler avec un tampon FIFO simple pour suivre le dernier segment sélectionné. Voici le code pour mon objet 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 

Un exemple de son 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 
Questions connexes