存储以前选择的UISegmentedControl片段?

时间:2009-07-27 18:35:46

标签: uikit uisegmentedcontrol

我需要跟踪以前选择的UISegmentControl片段。我可以使用委托方法吗?也许像selectedSegmentShouldChange:这样的东西?我能找到的唯一代理方法是segmentedControl:selectedSegmentChanged:。这个代表是我需要的一步之后。

1 个答案:

答案 0 :(得分: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