UISegmentdControl 类提供了一个多按钮的界面,用户可以从一组按钮中选择一个。该控件提供两种使用样式。
在正常的单选按钮样式模式下,一旦选中一个按钮,该按钮就一直处于选中状态。用户可以单击其他按钮,但它们
不能通过再次单击现有按钮来生成新事件。通过备用瞬时样式,用户可以多次单击每个按钮,但不存储当前选中项目的状态
它不突出显示最新选项。
代码: 创建响应第二次点击的分段控件子类
@class DoubleTapSegmentedControl;
@protocol DoubleTapSegmentedControlDelegate <NSObject>
- (void) performSegmentAction: (DoubleTapSegmentedControl *) aDTSC;
@end
@interface DoubleTapSegmentedControl : UISegmentedControl
{
id <DoubleTapSegmentedControlDelegate> delegate;
}
@property (nonatomic, retain) id delegate;
@end
@implementation DoubleTapSegmentedControl
@synthesize delegate;
// Catch touches and trigger the delegate
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if (self.delegate) [self.delegate performSegmentAction:self];
}
@end
@interface TestBedViewController : UIViewController <DoubleTapSegmentedControlDelegate>
@end
@implementation TestBedViewController
- (void) performSegmentAction: (DoubleTapSegmentedControl *) seg
{
char *s[3] = {"One", "Two", "Three"};
showAlert(@"Segment %s", s[seg.selectedSegmentIndex]);
}
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
self.title = @"Twice Tappable Segments";
// Build the control and set its delegate to this view controller
DoubleTapSegmentedControl *seg = [[[DoubleTapSegmentedControl alloc] initWithItems:[@"One Two Three" componentsSeparatedByString:@" "]] autorelease];
seg.center = CGPointMake(160.0f, 140.0f);
seg.delegate = self;
[self.view addSubview:seg];
}
@end