esreli
esreli

Reputation: 5051

Subclassed UIButton doesn't appear to initialize

This started as a simple problem, but I can't solve it. I have subclassed a UIButton (I know, controversial) but I can't seem to get it to work properly. My UIButton subclass looks like this:

.h

@interface SocialButton : UIButton 

 @property (nonatomic, strong) CustomClass *cust;

 - (id)initWithObject:(CustomClass *)custom andFrame:(CGRect)frame; 

@end

.m

@implementation SocialButton

@synthesize custom=_custom;

 - (id)initWithObject:(CustomClass *)custom andFrame:(CGRect)frame; 
 {
   self = [self initWithFrame:frame];
   if (self) 
   {        
     [self addTarget:self action:@selector(buttonTouchUpInside:)  forControlEvents:UIControlEventTouchUpInside];
     self.backgroundColor = [UIColor blueColor];
     self.cust = custom;
   }

   return self;
  }

@end

Later, when I try to create a button from this class, like so:

 SocialButton *social = [[SocialButton alloc] initWithObject:object andFrame:frame];

it doesn't appear to work. Any suggestions?

Upvotes: 0

Views: 233

Answers (1)

MByD
MByD

Reputation: 137272

Seems like you forgot to call super. Change:

self = [self initWithFrame:frame];

to:

self = [super initWithFrame:frame];

Upvotes: 1

Related Questions