0

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

#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

首先,当我在启动应用程序时将界面生成器中的类设置为一个按钮时,我没有看到任何东西,直到我点击了这个不可见的按钮。

第二件事是图像看起来比原始图像暗

帮助会很棒!谢谢!

4

1 回答 1

1

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

于 2013-04-08T07:06:52.270 回答