UIControlEventValueChanged not working when i tapped an already selected segment section

大憨熊 提交于 2019-12-25 04:58:11

问题


My aim is to trigger UIControlEventValueChanged delegate again when i tapped an already selected segment .

So i have referred the below links

uisegmentedcontrol-register-taps-on-selected-segment

maintaining-selection-in-uisegmentedcontrol

As 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 below

Main Class

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 file

#import <Foundation/Foundation.h>

@interface ReselectableSegmentControl : UISegmentedControl {    

}

@end

UISegmentSubClass .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.


回答1:


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;

    }
}


来源:https://stackoverflow.com/questions/11297745/uicontroleventvaluechanged-not-working-when-i-tapped-an-already-selected-segment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!