0

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?

Francis F
  • 3,157
  • 3
  • 41
  • 79

1 Answers1

0

Instead of
"UIControlEventTouchCancel" use
"UIControlEventTouchUpInside."

vaibhav_15
  • 351
  • 2
  • 9
  • I need to trigger the function only when button is released. Read my question fully. – Francis F Sep 09 '13 at 07:19
  • Some of the touch events don't work for UIButton. If you want to implement as per your requirement you can make use of touchesEnded method of UIResponder chain. – vaibhav_15 Sep 09 '13 at 07:43
  • how should I add touchesEnded to behave the way I desire, I tried something like this, but this didnt get fired when I clicked and release the button -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ forgotUnderline.highlighted=NO; } – Francis F Sep 09 '13 at 10:39
  • You have to add following lines to it.. UItouch * touch=[touches any object]; if(touch.view==your_button){forgotUnderline.highlighted=NO;} – vaibhav_15 Sep 10 '13 at 17:21
  • Yes, but still the touchesEnded method doesnt get fired when I release the button click. – Francis F Sep 11 '13 at 04:15