2012-08-23 27 views
0

创建了我自己的按钮的子类,并且我得到 - [UIRoundedRectButton setup]:发送到实例0x7c3e600'的无法识别的选择器。发送到UIButton实例的无法识别的选择器

我认为这是因为buttonWithType只是返回一个不明显类型的按钮,但不知道如何做到这一点!

@implementation OrangeButton 

    +(id)Create 
    { 
     OrangeButton *button = (OrangeButton*)[OrangeButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button setup]; 
     return button; 
    } 

    -(void) setup 
    { 
     [self setBG];  
    } 
    -(void)setBG 
    { 
     [self setBackgroundImage:[UIImage imageNamed:@"bg-button-orange.gif"] forState:UIControlStateNormal]; 
    } 

    @end 
+0

所以基本上,我只是围绕边界属性,而不是创建一个UiRoundedRectButton ...看起来很好,希望它确定 – Baconbeastnz

回答

1

我最近有类似的问题。 UIButton实际上是一个类集群 - 即当你实例化一个UIButton时,你可能会得到一个内部的Button类(可能根据按钮类型不同而不同,但这是一个实现细节)。有几个像这样的Apple类(NSData是另一个)。

不幸的是,这意味着它不是(现实的)子类化UIButton。如果您需要类似的功能,但不想/不能使用直UIButton,则带有附加UITapGestureRecogniser的UIView将成为我的第一个呼叫点。

编辑:

附加一个UITapGestureRecognizer到一个UIView提供非常相似的(和额外的额外,例如抽头可变数目)功能添加到一个UIButton。然而,而不是写以下内容:

[someButton addTarget:aTarget action:yourSelector forControlEvents:UIControlEventTouchUpInside]; 

您需要创建和附加手势识别:如果你想圆边

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:aTarget action:yourSelector]; 
yourUIView.userInteractionEnabled = YES; 
[yourUIView addGestureRecognizer:tgr]; 

,添加:

yourUIView.layer.cornerRadius = 5 // example value 

要获取您需要导入QuartzCore.h头的图层属性。

+0

- 如果你在RoundedRect看起来 - 你可以调整UIView图层的cornerRadius属性来获取正确的形状。 – Ephemera

1

我认为它可能的,因为我已创建自定义类

在.H,文件

@interface OrangeButton : UIButton 
{ 
} 
-(void) setBG; 
@end 

在.m文件

@implementation OrangeButton 

- (id) initWithFrame: (CGRect)frame 
{ 
    self = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 

    // set frame 
    self. frame = frame; 

    if (self) 
    { 
     // change bg color 
     [self setBG]; 

     return self; 
    } 

    return nil; 
} 

-(void) setBG 
{ 
    [self setBackgroundImage:[UIImage imageNamed:@"bg-button-orange.gif"] forState:UIControlStateNormal]; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 

现在,当你想使用它,致电

OrangeButton *obj= [[OrangeButton alloc] initWithFrame: CGRectMake(0, 0, 90, 40)]; 
[obj setContentMode: UIViewContentModeScaleAspectFit]; 

// add it to the view 
[self addSubview: obj]; 

// assign action 
[obj addTarget: self action: someAction forControlEvents: UIControlEventTouchUpInside]; 

我觉得这个将做它

相关问题