Skip to content

Instantly share code, notes, and snippets.

@startupcode
Created July 8, 2016 08:32
Swift - Extend UIButton to save an object
private var associationKey: UInt8 = 0
extension UIButton {
var objectRef: String! {
get {
return objc_getAssociatedObject(self, &associationKey) as? String
}
set(newValue) {
objc_setAssociatedObject(self, &associationKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
}
//How to use it
var btnObj = UIButton()
btnObj.objectRef = object // This can be a viewcontroller, navigationcontroller etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment