gpt4 book ai didi

iphone - 将简单 UIButton 子类化为复选框

转载 作者:行者123 更新时间:2023-11-29 04:00:46 24 4
gpt4 key购买 nike

我正在子类化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

首先,当我启动应用程序时将界面生成器中的类设置为按钮时,我什么也看不到,直到我点击这个不可见的按钮。

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

帮助会很棒!谢谢!

最佳答案

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

关于iphone - 将简单 UIButton 子类化为复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15872909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com