在创建 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/