I'm really new on Objective-C and iOS. I'm having a problem:
I'm creating buttons dynamically and I need to store some data on the buttons to use it on the onClick
method. I have a class for storing my data and the problem starts when trying to store that data on the button.
Here is the data class .h to be stored:
@interface MenuItem : NSObject
{
NSString *name;
NSString *action;
NSString *xml;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *action;
@property (nonatomic, retain) NSString *xml;
@end
This class does not have any method.
Here is my button class:
@interface MyButton : UIButton
{
MenuItem * buttonInf;
}
@property (nonatomic, retain) MenuItem * buttonInf;
@end
I'm having a problem when putting the data into that field:
MyButton * button = [MyButton buttonWithType:UIButtonTypeRoundedRect];
button.buttonInf = buttoninf;
I tried doing:
[button setButtonInf:buttoninf]
and making that setter, but same result, always having:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [UIRoundedRectButton setButtonInf:]: unrecognized selector sent to instance
and I really dont know why.
As the documentation say:
Discussion
This method is a convenience constructor for creating button objects with specific configurations. It you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.
That means that the object returned by buttonWithType:
is of type of UIButton
(not MyButton
) and this class do not declare the buttonInf
property.
You will have to create your button in a different way (using initWithFrame
for example) and then your QuartzCore
configure the aspect of the button to looks like you want.
Using the code above I get the right button shown in the image
MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(100, 20, 73, 44)];
button.layer.borderColor = [UIColor lightGrayColor].CGColor;
button.layer.borderWidth = 1.0;
button.layer.cornerRadius = 10.0;
[button setTitle:@"Button" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.