UIControlEventTouchCancel not triggering the function

667 views Asked by At

I have two buttons in my xib file. This is my interface file

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *testButtonOut;
@property (strong, nonatomic) IBOutlet UIButton *button2;

- (IBAction)testButton:(id)sender;
- (void)buttonReleased;

@end

These are the implementations of two functions in my implementation file

- (IBAction)testButton:(id)sender {
    button2.highlighted=YES;
   [testButtonOut addTarget:self action:@selector(buttonReleased) forControlEvents:UIControlEventTouchCancel];
}

- (void)buttonReleased{
    button2.highlighted=NO;
}

When I click on the testButton and release, it does not trigger the buttonReleased function (I used breakpoint to check it), but if I change UIControlEventTouchCancel to UIControlEventTouchUpInside it does trigger the buttonReleased function when I click on it but I dont want to trigger it on button click I want to trigger it on button release, I also tried with TouchUpOutside but that didnt trigger as well. Which event should I use so that it calls the buttonReleased function when I release the click from the button event?

1

There are 1 answers

5
vaibhav_15 On

Instead of
"UIControlEventTouchCancel" use
"UIControlEventTouchUpInside."