I'd like to subclass UIButton
to add some properties that i need (not methods... only properties).
Here the code of my subclass:
//.h-----------------------
@interface MyButton : UIButton{
MyPropertyType *property;
}
@property (nonatomic,retain) MyPropertyType *property;
@end
//.m--------------------------
@implementation MyButton
@synthesize property;
@end
And here how I use the class:
MyButton *btn = ((MytButton *)[MyButton buttonWithType:UIButtonTypeRoundedRect]);
btn.property = SomeDataForTheProperty;
From where i obtain this error :
-[UIRoundedRectButton setProperty:]: unrecognized selector sent to instance 0x593e920
Thus, from ButtonWithType
i obtain a UIRoundedRectButton
and (Mybutton *)
can't cast it...
What i have to do to obtain a MyButton
object ? is -init
the unique solution ?
Thank you!
I have a simple scheme that only involves a few library methods, no boilerplate, and just 3 lines of code for each property you want to add. There are two example properties added below: startPoint and tileState. For illustrative purposes here are the lines you'd need to add for a property like tileState:
There's more details in my blog post describing how this works
UIButton+SCZButton.h
UIButton+SCZButton.m
OOGTotallyTile.h
OOGTotallyTile.m
Try using a category with Associative References instead. It is much cleaner and will work on all instances of
UIButton
.UIButton+Property.h
UIButton+Property.m
//Example usage
You need to do:
To create your button. The
buttonWithType:UIButtonTypeRoundedRect
only creates UIButton objects.=== edit ===
If you wish to use a RoundedRect button; then I would suggest the following. Basically, we just create a UIView with whatever properties we want and add the desired button to that view.
.h
.m
Usage: