I think I’ve followed sample code to the letter, but the below is giving me an error.
I want to subclass UIButton and add a couple of properties, but I’m failing from the get-go.
I have created a subclass file. these are my .h/.m’s:
// damButton.h
#import <UIKit/UIKit.h>
@interface damButton : UIButton
{
CGFloat _position;
}
@property (nonatomic) CGFloat position;
@end
and
// damButton.m
#import "damButton.h"
@implementation damButton
@synthesize position = _position;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
@end
in my mainviewcontroller, I have imported my custom button, but when I use the property’s inbuilt getter and setter, I get an error:
//MainViewController.m
#import "damButton.h"
// then within a method...
damButton *b = [damButton buttonWithType:UIButtonTypeRoundedRect];
[b position:5.0];
generates this error: No visible @interface for 'damButton' declares the selector 'position:'
I’m not sure what I’m missing here, I’ve pretty much copied it verbatim (I think). I only want to use the inbuilt getters/setters (for now).
What am I missing?
You are calling the getter method, instead of the setter method -setPosition, i.e. try:
or
May I ask what you’re trying to achieve by subclassing UIButton?