我是UIButton的子类,我想要的是将按钮类型设置为Round Rect.
Button.h
@interface Button : UIButton {}
- (void)initialize;
@end
Run Code Online (Sandbox Code Playgroud)
Button.m
@implementation Button
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self){
[self initialize];
}
return self;
}
- (void)initialize
{
self.titleLabel.font = [UIFont systemFontOfSize:20];
self.titleLabel.textColor = [UIColor redColor];
self.titleLabel.textAlignment = UITextAlignmentCenter;
//[UIButton buttonWithType:UIButtonTypeRoundedRect];
}
@end
Run Code Online (Sandbox Code Playgroud)
在这里我尝试[UIButton buttonWithType:UIButtonTypeRoundedRect]但它不起作用.任何人都可以建议如何使其工作?
我在之前的许多文章中都知道,不推荐使用Subclassing UIButton,但事实上在Developer's Docs中没有提及NOT子类化它.
任何人都可以解释为什么我们需要包含if (self == SomeClass class)在+initialize方法内?
我发现了类似的问题(如下所列),但没有找到任何具体的说明:
每个人都说如果你没有+initialize在Subclass中实现/覆盖,那么它将调用父类两次.
任何人都可以特别解释这一部分,特别是为什么它会两次调用父类?
最后,当我们+initialize在继承自NSObject的类中实现时,为什么不会发生这种情况,我们在其中创建自定义-init方法并调用self = [super init];.