David West
David West

Reputation: 562

Subclassing UIButton

I would like to know if you think subclassing UIButton would be a good idea in my situation and if so how exactly to go about doing it?

So the situation is that I have three custom buttons in my project that I need in most if not all view controllers in my project. The approach I have taken at the moment is that I've built them using storyboards in all my controllers and then added functionality as needed. However, I've found that this can lead to a painful situation where even when I want to do something, like just change the height, I need to do it 8 times. So my idea was this, have 3 subclasses of uibuttons (with the required characteristics like image in different states,width,height,position etc) that I can then add to my different view controllers and then perform segues from them as and when needed. And if I need to make any changes, I will just have to do them once in the subclass as opposed to in all the view controllers.

What do you guys think?

Upvotes: 2

Views: 6645

Answers (2)

rdelmar
rdelmar

Reputation: 104082

Sure, I think this would work fine. Add custom buttons in your storyboard, and change their class to one of your button subclasses. In the subclass make your customizations in the initWithCoder: method.

Upvotes: 7

NDM
NDM

Reputation: 944

You can subclass the UIButton, but a better solution would be to create a Category for the UIButton

look at the tutorial: http://mobile.tutsplus.com/tutorials/iphone/custom-uibutton_iphone/

Upvotes: 0

Related Questions