From 1a2509e0873c26a10a7a85a4c93564cf007293f9 Mon Sep 17 00:00:00 2001 From: Tim Oliver Date: Sun, 8 Oct 2023 10:18:06 +0900 Subject: [PATCH] Added a delegate property to receive button tap events --- CHANGELOG.md | 1 + TORoundedButton/TORoundedButton.h | 13 +++++++++++++ TORoundedButton/TORoundedButton.m | 3 ++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de53900..2b9d411 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ x.y.z Release Notes (yyyy-MM-dd) ### Added +* A `delegate` property to receive tap events for the button where delegates are preferred over blocks. * An `isTranslucent` property (and a `blurStyle` property) that replaces the background of buttons from a solid color to a blurred background. * A `contentView` property to enable adding custom view content to buttons. * `sizeToFit` and `sizeThatFits:` methods to allow automatic sizing of buttons around their content. diff --git a/TORoundedButton/TORoundedButton.h b/TORoundedButton/TORoundedButton.h index 3a6a5e6..cb1d954 100644 --- a/TORoundedButton/TORoundedButton.h +++ b/TORoundedButton/TORoundedButton.h @@ -24,9 +24,22 @@ NS_ASSUME_NONNULL_BEGIN +@class TORoundedButton; + +@protocol TORoundedButtonDelegate + +/// Called when the user taps on the associated button. +/// - Parameter button: The button object that was tapped. +- (void)roundedButtonDidTap:(TORoundedButton *)button; + +@end + NS_SWIFT_NAME(RoundedButton) IB_DESIGNABLE @interface TORoundedButton : UIControl +/// A delegate object that can receive tap events from this button. +@property (nonatomic, weak) id delegate; + /// The radius of the corners of this button (Default is 12.0f). @property (nonatomic, assign) IBInspectable CGFloat cornerRadius; diff --git a/TORoundedButton/TORoundedButton.m b/TORoundedButton/TORoundedButton.m index e774ce7..4053796 100644 --- a/TORoundedButton/TORoundedButton.m +++ b/TORoundedButton/TORoundedButton.m @@ -282,8 +282,9 @@ - (void)_didTouchUpInside { // Send the semantic button action for apps relying on this action [self sendActionsForControlEvents:UIControlEventPrimaryActionTriggered]; - // Trigger the block if it has been set + // Broadcast the tap event to all subscribed objects. if (self.tappedHandler) { self.tappedHandler(); } + [_delegate roundedButtonDidTap:self]; } - (void)_didDragOutside {