fscore fscore - 9 months ago 32
iOS Question

How to pass Int64/Int32 values through UIButton Swift

I want to pass a Int64/32 values from my object to a button for it to perform further operation. But

takes a
value so it gives a error and if I change
then it only takes the first few numbers from the number.

cellBtn = UIButton(frame: CGRect(x: 150, y: 30, width: 100, height: 100))
cellBtn.backgroundColor = .greenColor()
cellBtn.setTitle("Test", forState: .Normal)

//obj.id is Int64 value
cellBtn.tag = obj.id

cellBtn.addTarget(self, action: #selector(btnU), forControlEvents: .TouchUpInside)

return cell!

func btnU(sender: UIButton!) {


How can I pass
from the
cellBtn to btnU
so btnU has information from

  1. I would not recommend passing values on UI elements, you should have model object for that purpose.

  2. I would not recommend using the "tag" property since I'ts too generic. You never know witch framework will use and change the same tag value as you do.

But if you must, use a subclass, that way you can use Int64:

class ByButton: UIButton {
    var myValue: Int64 = 0

Or you can add run time property to all UIButtons (This is less recommended from the first solution since I'ts hard to find, and will add on all UIButtons in your application)

private var buttonValue: Int64 = 0
extension UIButton {
    var keyBoardHeight: CGFloat {
        get {
            return objc_getAssociatedObject(self, &buttonValue) as! CGFloat
        set { objc_setAssociatedObject(self, &buttonValue, newValue,  objc_AssociationPolicy(rawValue: 0)!) }