SHARE
TWEET

Untitled

a guest May 21st, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. protocol CartButtonProtocol: AnyObject {
  2.     var cartBarButton: UIBarButtonItem? { get set }
  3. }
  4.  
  5. private var UIBarButtonKey: UInt8 = 0
  6.  
  7. extension CartButtonProtocol {
  8.    
  9.     var cartBarButton: UIBarButtonItem? {
  10.         get {
  11.             return objc_getAssociatedObject(self, &UIBarButtonKey) as? UIBarButtonItem
  12.         } set {
  13.             guard let newValue = newValue else { return }
  14.             objc_setAssociatedObject(
  15.                 self,
  16.                 &UIBarButtonKey,
  17.                 newValue as AnyObject,
  18.                 objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC
  19.             )
  20.         }
  21.     }
  22. }
  23.  
  24. extension UIViewController: CartButtonProtocol {
  25.    
  26.     func configureCartBarButtonItem() {
  27.         let image = Asset.icShoppingCartWhite.image.resizeImage(withScale: 0.5)
  28.         let badgeButton = UIButton()
  29.         badgeButton.setImage(image, for: .normal)
  30.         badgeButton.addTarget(self, action: #selector(didTappedCartButton(_:)), for: .touchUpInside)
  31.         cartBarButton = UIBarButtonItem(customView: badgeButton)
  32.         navigationItem.rightBarButtonItem = cartBarButton
  33.     }
  34.  
  35.     @objc
  36.     private func didTappedCartButton(_ sender: UIBarButtonItem) {
  37.     }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top