Reputation: 1
I'm new to Objective-C and want to be able to attach an integer attribute to each physical button I can see in Interface Builder; I'll also need many other variables so just using the 'Tag' attribute is not sufficient. I've created a sub-class but can't seem to alter these new variables from instances of this class.
myButton.h---------
@interface myButton : UIBUtton
{
int hiddenNumber;
}
@property(nonatomic, assign) int hiddenNumber;
myButton.m--------
#import "myButton.h"
@implementation myButton
@synthesize hiddenNumber;
ViewController.h-------
IBOutlet myButton *button1; // This has been connected in Interface Builder.
ViewController.m-------
[button1 setAlpha:0]; // This works (one of the built-in attributes).
[button1 setHiddenNumber:1]; // This won't (one of mine)! It receives a 'Program received signal: "SIGABRT".
Any help would be great, thanks.
Upvotes: 0
Views: 196
Reputation: 57179
The problem with subclassing UIButton just to add properties to store data is you end up limiting yourself to only the custom button type. No more rounded rect for you since these buttons are part of class cluster. My recommendation? use Associative References. Take a look at this post Subclass UIButton to add a property
UIButton+Property.h
#import <Foundation/Foundation.h>
@interface UIButton(Property)
@property (nonatomic, retain) NSObject *property;
@end
UIButton+Property.m
#import "UIButton+Property.h"
#import <objc/runtime.h>
@implementation UIButton(Property)
static char UIB_PROPERTY_KEY;
@dynamic property;
-(void)setProperty:(NSObject *)property
{
objc_setAssociatedObject(self, &UIB_PROPERTY_KEY, property, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSObject*)property
{
return (NSObject*)objc_getAssociatedObject(self, &UIB_PROPERTY_KEY);
}
@end
Upvotes: 1
Reputation: 16540
In Interface Builder you will have to set the type of the Button to your custom button.
Under "Identity Inspector" is Custom Class. Set that from UIButton to myButton.
Upvotes: 3