Subclassed UIButton似乎没有初始化

时间:2012-05-01 00:14:06

标签: iphone objective-c uibutton subclass

这开始是一个简单的问题,但我无法解决它。我已经将UIButton(我知道,有争议)分类,但我似乎无法让它正常工作。我的UIButton子类看起来像这样:

·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

稍后,当我尝试从这个类创建一个按钮时,如下所示:

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

它似乎不起作用。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

好像忘了给super打电话。变化:

self = [self initWithFrame:frame];

为:

self = [super initWithFrame:frame];