From b84771d4a62aa7da0a045c534f4e4fa134617aea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Derache?= Date: Thu, 5 May 2022 15:57:37 +0200 Subject: [PATCH] Add delegate --- Sources/M13Checkbox.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Sources/M13Checkbox.swift b/Sources/M13Checkbox.swift index 0e20060..3a5eb89 100644 --- a/Sources/M13Checkbox.swift +++ b/Sources/M13Checkbox.swift @@ -13,6 +13,18 @@ import UIKit +/// The delegate protocol fires when check and uncheck the box. +public protocol M13CheckboxDelegate: class { + + /// Called when check the M13CheckBox + /// - Parameter checkBox: Current M13CheckBox object + func checked(_ checkBox:M13Checkbox) + + /// Called when uncheck the M13CheckBox + /// - Parameter checkBox: Current M13Checkbox object + func unchecked(_ checkBox:M13Checkbox) +} + /// A customizable checkbox control for iOS. @IBDesignable open class M13Checkbox: UIControl { @@ -21,6 +33,9 @@ open class M13Checkbox: UIControl { // MARK: - Constants //---------------------------- + /// The M13Checkbox delegate. + weak public var delegate: M13CheckboxDelegate? + /** The possible states the check can be in. @@ -319,9 +334,11 @@ open class M13Checkbox: UIControl { open func toggleCheckState(_ animated: Bool = false) { switch checkState { case .checked: + delegate?.unchecked(self) setCheckState(.unchecked, animated: animated) break case .unchecked: + delegate?.checked(self) setCheckState(.checked, animated: animated) break case .mixed: