0

我的目标是在我点击已选择的段时再次触发 UIControlEventValueChanged委托。

所以我参考了以下链接

uisegmentedcontrol-register-点击选定的段

维护选择中的分段控制

根据链接,我创建了一个 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。

对此问题的任何帮助表示赞赏。

谢谢。

4

1 回答 1

5

呼...终于我得到了解决方案..

我使用了 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;

    }
}
于 2012-07-03T10:22:15.627 回答