maintaining-selection-in-uisegmentedcontrol根据链接,我创建了一个uisegmentedcontrol子类,实现了- (void) setSelectedSegmentIndex:(NSInteger)toValue {方法并编写了代码(来自上述链接).它可以在Ipad1上正常工作.但是,当点击已选择的段时,继承/子类中的 setSelectedSegmentIndex 不会在Ipad2中触发!!!但是,当我点击一个未选择的段索引时,它运行良好.我完成的代码如下 主班 ReselectableSegmentControl *firstNextSegmentedControl = [[ReselectableSegmentControl alloc] init];firstNextSegmentedControl.frame = CGRectMake(xCordinate, 255, 188, 50);firstNextSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;firstNextSegmentedControl.tag = index + 1;[firstNextSegmentedControl addTarget:self action:@selector(firstNextThenSegmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"NoTimerCounter.png"] atIndex:0 animated:YES];[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"Timer.png"] atIndex:1 animated:YES];[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"Counter.png"] atIndex:2 animated:YES];firstNextSegmentedControl.selectedSegmentIndex = 0;[self.view addSubview:firstNextSegmentedControl];[firstNextSegmentedControl release];firstNextSegmentedControl = nil; UISegmentSubClass .h文件 #import <Foundation/Foundation.h>@interface ReselectableSegmentControl : UISegmentedControl {}@end UISegmentSubClass .m文件 -(void)setSelectedSegmentIndex:(NSInteger )selectedValue{ if (selectedValue==self.selectedSegmentIndex) { [self sendActionsForControlEvents:UIControlEventValueChanged]; } [super setSelectedSegmentIndex:selectedValue];}或-(void)setSelectedSegmentIndex:(NSInteger )selectedValue{ if (self.selectedSegmentIndex == selectedValue) { //doing my operation if tapped again the selected value. else { super.selectedSegmentIndex = selectedValue; }}或- (void) setSelectedSegmentIndex:(NSInteger)toValue { if (self.selectedSegmentIndex == toValue) { [super setSelectedSegmentIndex:UISegmentedControlNoSegment]; } else { [super setSelectedSegmentIndex:toValue]; }}以上所有方法均不适用于Ipad2.在此问题上的任何帮助都将受到赞赏.谢谢.解决方案 Phew ...终于我得到了解决方案..我使用了 UITapGestureRecognizer ,而不是使用了addTarget:self action:@selector()现在我的代码看起来像UISegmentedControl *firstNextSegmentedControl = [[UISegmentedControl alloc] init];firstNextSegmentedControl.frame = CGRectMake(xCordinate, 255, 188, 50);firstNextSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;firstNextSegmentedControl.selectedSegmentIndex = 0;[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"NoTimerCounter.png"] atIndex:0 animated:YES];[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"Timer.png"] atIndex:1 animated:YES];[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"Counter.png"] atIndex:2 animated:YES]; //Added UITapGestureRecognizer instead of using addTarget: method UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init]; tapGesture addTarget:self action:@selector(firstNextThenSegmentedControlValueChanged:) ]; [firstNextSegmentedControl addGestureRecognizer:tapGesture]; [tapGesture release]; tapGesture = nil;[self.view addSubview:firstNextSegmentedControl];[firstNextSegmentedControl release];firstNextSegmentedControl = nil;-(void)firstNextThenSegmentedControlValueChanged:(UITapGestureRecognizer *)tapGesture{ UISegmentedControl *segmentControl = (UISegmentedControl *)tapGesture.view; switch (segmentControl.selectedSegmentIndex) { case 0: [self doIt:segmentControl]; break; default: break; }}My aim is to trigger UIControlEventValueChanged delegate again when i tapped an already selected segment .So i have referred the below linksuisegmentedcontrol-register-taps-on-selected-segmentmaintaining-selection-in-uisegmentedcontrolAs per the links i have created a uisegmentedcontrol subclass , implemented the - (void) setSelectedSegmentIndex:(NSInteger)toValue { method and wrote the codes (from the above links).Its worked fine with Ipad1. However the setSelectedSegmentIndex in the inherited/sub class is not triggering in Ipad2 when tapped an already selected segment!!!! however it is working fine when i tapped an unselected segment index.The code i done is belowMain ClassReselectableSegmentControl *firstNextSegmentedControl = [[ReselectableSegmentControl alloc] init];firstNextSegmentedControl.frame = CGRectMake(xCordinate, 255, 188, 50);firstNextSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;firstNextSegmentedControl.tag = index + 1;[firstNextSegmentedControl addTarget:self action:@selector(firstNextThenSegmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"NoTimerCounter.png"] atIndex:0 animated:YES];[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"Timer.png"] atIndex:1 animated:YES];[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"Counter.png"] atIndex:2 animated:YES];firstNextSegmentedControl.selectedSegmentIndex = 0;[self.view addSubview:firstNextSegmentedControl];[firstNextSegmentedControl release];firstNextSegmentedControl = nil;UISegmentSubClass .h file#import <Foundation/Foundation.h>@interface ReselectableSegmentControl : UISegmentedControl {}@endUISegmentSubClass .m file-(void)setSelectedSegmentIndex:(NSInteger )selectedValue{ if (selectedValue==self.selectedSegmentIndex) { [self sendActionsForControlEvents:UIControlEventValueChanged]; } [super setSelectedSegmentIndex:selectedValue];}or-(void)setSelectedSegmentIndex:(NSInteger )selectedValue{ if (self.selectedSegmentIndex == selectedValue) { //doing my operation if tapped again the selected value. else { super.selectedSegmentIndex = selectedValue; }}or- (void) setSelectedSegmentIndex:(NSInteger)toValue { if (self.selectedSegmentIndex == toValue) { [super setSelectedSegmentIndex:UISegmentedControlNoSegment]; } else { [super setSelectedSegmentIndex:toValue]; }}The all above methods are not working with Ipad2.Any help on this issue is appreciated.Thanks. 解决方案 Phew... finally i got the solution..I have used UITapGestureRecognizer instead of using addTarget:self action:@selector()Now my code is looking like,UISegmentedControl *firstNextSegmentedControl = [[UISegmentedControl alloc] init];firstNextSegmentedControl.frame = CGRectMake(xCordinate, 255, 188, 50);firstNextSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;firstNextSegmentedControl.selectedSegmentIndex = 0;[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"NoTimerCounter.png"] atIndex:0 animated:YES];[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"Timer.png"] atIndex:1 animated:YES];[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"Counter.png"] atIndex:2 animated:YES]; //Added UITapGestureRecognizer instead of using addTarget: method UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init]; tapGesture addTarget:self action:@selector(firstNextThenSegmentedControlValueChanged:) ]; [firstNextSegmentedControl addGestureRecognizer:tapGesture]; [tapGesture release]; tapGesture = nil;[self.view addSubview:firstNextSegmentedControl];[firstNextSegmentedControl release];firstNextSegmentedControl = nil;-(void)firstNextThenSegmentedControlValueChanged:(UITapGestureRecognizer *)tapGesture{ UISegmentedControl *segmentControl = (UISegmentedControl *)tapGesture.view; switch (segmentControl.selectedSegmentIndex) { case 0: [self doIt:segmentControl]; break; default: break; }} 这篇关于当我点击一个已经选择的段部分时,UIControlEventValueChanged无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!