I’ve created a custom button called TaskUIButton that inherits from UIButton. The only difference I have right now is a “va” property.
Here’s the interface
// TaskUIButton.h
@interface TaskUIButton : UIButton
{
NSString *va;
}
@property(nonatomic, retain) NSString *va;
@end
And the implementation file
//TaskUIButton.m
@implementation TaskUIButton
@synthesize va;
@end
Now, I’ve got an action that I’m using which I want to use to set and retrieve the va property of a button (just for testing/experimentation of course).
Here is where the button action is
- (IBAction)setAndRetrieveVa:(id)sender{
TaskUIButton *imaButton = [TaskUIButton buttonWithType:UIButtonTypeRoundedRect];
imaButton.va = @"please work";
NSLog(@"%@", imaButton.va);
}
Upon activating the setAndRetrieveVa: action, my app crashes with:
-[UIRoundedRectButton setVa:]: unrecognized selector sent to instance 0x4b3a5a0
I’m sure that its a stupid mistake on my part, but I’ve been going at it for a while and would love some insight!
Thanks!
You are getting this because
buttonWithType:is returning a new object which is aUIRoundedRectButtonobject which is a subclass ofUIButton. You can’t alter this behavior of the method unless you override but you are unlikely to get what you want. You should take thealloc-initapproach.Using Associative References
You will need to
#import <Foundation/NSObjCRuntime.h>for this to work.To set,
And to retrieve,
This way you wouldn’t need to subclass
UIButton.