gpt4 book ai didi

iphone - 为 UIButton 设置一个子类来处理外观

转载 作者:行者123 更新时间:2023-11-28 20:14:44 25 4
gpt4 key购买 nike

我有一个 AuthButtons.h header :

#import <UIKit/UIKit.h>

@interface AuthButtons : UIButton

@property (nonatomic, weak) UIColor *backgroundColor;


@end

AuthButtons.m类的实现如下:

#import "AuthButtons.h"

@implementation AuthButtons

- (void)setBackgroundColor:(UIColor *)backgroundColor
{
backgroundColor = [UIColor blueColor];
}

- (UIColor *)backgroundColor
{
return [UIColor whiteColor];
}


- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}

然后在我的 viewcontroller.m 类中我有:

@interface ViewController ()

@property (nonatomic, weak) AuthButtons *registerButton;


@end


...

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

_registerButton = [AuthButtons buttonWithType:UIButtonTypeSystem];
[_registerButton setTitle:@"Register" forState:UIControlStateNormal];
[_registerButton setBackgroundColor:[UIColor whiteColor]];
_registerButton.frame = CGRectMake(0.0, 0.0, self.screenWidth / 2.0, 100.0);

[_registerButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}

当我尝试创建按钮时它总是崩溃。我错过了什么?

最佳答案

你的按钮很弱,没有添加为 subview ,所以它立即被释放

关于iphone - 为 UIButton 设置一个子类来处理外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18502411/

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