子类简单的UIButton复选框

时间:2013-04-08 06:48:57

标签: iphone ios uibutton uikit

我正在为UIButton创建一个简单的Checkbox行为。这是代码:

#import "RadioButton.h"

@implementation RadioButton
@synthesize isSelected;

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self ChangeButtonState];
 }

  -(void)ChangeButtonState{
  if (!isSelected) {
    isSelected = YES;
    [self setBackgroundImage:[UIImage imageNamed:@"radiobtn_on.png"]      forState:UIControlStateNormal];
    }
    else{
    isSelected = NO;
    [self setBackgroundImage:[UIImage imageNamed:@"radiobtn_off.png"] forState:UIControlStateNormal];
     }
     }

    -(id)init{
     self.adjustsImageWhenHighlighted=YES;
    self.alpha = 1;
     [self ChangeButtonState];
    isSelected = NO;
    [self setBackgroundImage:[UIImage imageNamed:@"radiobtn_off.png"] forState:UIControlStateNormal];
return self;
    }

    - (id)initWithFrame:(CGRect)frame
     {

    [self setBackgroundImage:[UIImage imageNamed:@"radiobtn_off.png"] forState:UIControlStateNormal];
    isSelected = NO;
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code
     }
     return self;
      }

    /*
  // Only  override drawRect: if you perform custom drawing.
   // An empty implementation adversely affects performance during animation.
     - (void)drawRect:(CGRect)rect
    {
       // Drawing code
     }
     */

     @end

首先,当我在启动应用程序时将界面构建器中的类设置为按钮时,我看不到任何内容,直到我点击此隐形按钮。

另一件事就是图像看起来比原来的图像暗了

帮助会很棒! 谢谢!

1 个答案:

答案 0 :(得分:1)

尝试你的东西 - (void)awakeFromNib。在您的子类中定义此方法。当您在xib中分配自定义类时,将调用此方法并在那里应用您的逻辑。