2010-05-31 67 views
0

比如我想从UIButton的覆盖:如何以正确的方式覆盖便利的构造函数?

+ (id)buttonWithType:(UIButtonType)buttonType 

所以我会做:

+ (id)buttonWithType:(UIButtonType)buttonType { 
    UIButton *button = [UIButton buttonWithType:buttonType]; 
    if (button != nil) { 
     // do own config stuff ... 
    } 
    return button; 
} 

是正确的方式?或者我错过了什么? (是的,我已经覆盖了数以千计的实例方法,但从来没有类方法;))

回答

1

所以你有递归。

不幸的是,您不能使用buttonWithType以外的方法创建指定类型的按钮。如果您需要在创建后初始化按钮,您可以制作自己的静态方法:

+(id)buttonWithTypeEx:(UIButtonType)buttonType { 
    UIButton *button = [UIButton buttonWithType:buttonType]; 
    if (button != nil) { 
     // do own config 
    } 
    return button; 
}