My custom button touchupinside event not fire when I put some code in my custom button
Note: When I tap on button this method is fire but touchUpInside method not fire
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"touchBegan");
[Singleton playSoundForEvent:Sound_ButtonTap];
}
If I remove above code then working fine.
Above code reason : I don't put tap sound everywhere on touchupinside. I would like only One line code in whole project.
答案 0 :(得分:1)
You forgot the call to super
[super touchesBegan:touches withEvent:event];
The reason it doesn't work without the call to super, is that a button needs to register touch events (touchesBegan
, touchesEnded
, touchesMoved
, touchesCanceled
) to be able to map them to UIControlEvent
, and only then fire actions with sendAction
.
Full code
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(@"touchBegan");
[Singleton playSoundForEvent:Sound_ButtonTap];
}
答案 1 :(得分:0)
Please check this code and test again I hope It will working fine.
[super touchesBegan:touches withEvent:event];