Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set custom attribute on button

How can i set custom attributes on an button that is programmatically created?

I have this code to create an button:

var btn = UIButton(frame: CGRectMake(CGFloat(7 + add), 565, 39, 55))     
            btn.setTitle(String(Array(shuffledWord)[i...i]), forState: UIControlState.Normal)
            btn.addTarget(self, action: "btnPressed:", forControlEvents: UIControlEvents.TouchUpInside)
 btn.tag = 7 + add;

I can give some value to .tag but are there more thing like that? Can i create custom attributes to set on the button?

like image 808
da1lbi3 Avatar asked Jul 30 '26 14:07

da1lbi3


1 Answers

If you need custom properties on a class that doesn't belong to you, subclass it and use the subclass instead.

class MyCoolButton : UIButton {
    var myCoolProperty : String = "coolness"
}

And later:

var btn = MyCoolButton( // ...
like image 152
matt Avatar answered Aug 02 '26 07:08

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!