Last active
November 28, 2020 16:25
-
-
Save lamprosg/92bcc3f0bd336c89147e95fcb2cfce4e to your computer and use it in GitHub Desktop.
(iOS) Swift button tap with handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MySwitch: UISwitch { | |
| typealias DidTapSwitch = (WMSwitch) -> () | |
| var didChangeState: DidTapSwitch? { | |
| didSet { | |
| if didChangeState != nil { | |
| addTarget(self, action: #selector(self.switchStateDidChange(_:)), for: .valueChanged) | |
| } else { | |
| removeTarget(self, action: #selector(self.switchStateDidChange(_:)), for: .valueChanged) | |
| } | |
| } | |
| } | |
| @objc func switchStateDidChange(_ sender: MySwitch) { | |
| if let handler = didChangeState { | |
| handler(self) | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it:
Remove action