当我点击已经选择的段部分时,UIControlEventValueChanged不起作用

时间:2012-07-02 16:39:36

标签: iphone objective-c uisegmentedcontrol ipad-2 uicontrolevents

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

所以我已经提到了以下链接

uisegmentedcontrol-register-taps-on-selected-segment

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。

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

感谢。

1 个答案:

答案 0 :(得分:5)

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;

    }
}