2

我需要跟踪以前选择的 UISegmentControl 段。有我可以使用的委托方法吗?也许像selectedSegmentShouldChange:什么?我能找到的唯一委托方法是segmentedControl:selectedSegmentChanged:. 这位代表是我需要的代表之后的一步。

4

1 回答 1

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
于 2010-05-28T15:45:07.707 回答