1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import Foundation
- import UIKit
- private var actionKey: Void?
- extension UIBarButtonItem {
-
- private var _action: () -> Void {
- get {
- return objc_getAssociatedObject(self, &actionKey) as? () -> Void ?? { }
- }
- set {
- objc_setAssociatedObject(self, &actionKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- }
- }
- convenience init(title: String?, style: UIBarButtonItem.Style, action: @escaping () -> Void) {
- self.init(title: title, style: style, target: nil, action: #selector(pressed))
- self.target = self
- self._action = action
- }
- convenience init(image: UIImage?, style: UIBarButtonItem.Style, action: @escaping () -> Void) {
- self.init(image: image, style: style, target: nil, action: #selector(pressed))
- self.target = self
- self._action = action
- }
- @objc private func pressed(sender: UIBarButtonItem) {
- _action()
- }
- }
|