gpt4 book ai didi

objective-c - 自定义 cocoa 对象的默认值

转载 作者:行者123 更新时间:2023-12-03 17:47:47 25 4
gpt4 key购买 nike

在创建 MyLabel 时,我总是设置一些参数,而不是每次都必须将它们写出来,我宁愿将它们设置为自定义类的默认值。

我试过了,但没有成功:

@interface MyLabel: NSTextField
@end

@implementation MyLabel
-(id)init {
if (self = [super init]) {
[self setWantsLayer:YES];
[self setSelectable:YES];
[self setEditable:NO];
[self setBordered:NO];
}
return self;
}
@end

只是没有调用 init。

MyLabel 的调用方式如下:

MyLabel* error_label = [[MyLabel alloc] initWithFrame: ...

最佳答案

不要使用initWithFrame。仅使用 init 进行调用。

尝试下面的代码

MyLabel* error_label = [[MyLabel alloc] init];

您可以在初始化后设置框架,如下所示

[error_label setFrame::CGRectMake(0, 0, 0, 0)]; // set co-ordinates accordingly

关于objective-c - 自定义 cocoa 对象的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45434225/

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