如果我子类UIButton
并使用其buttonWithType:
方法创建button
,它是否返回我UIButton
或子类实例?
@interface MyButton : UIButton
@end
MyButton *button = [MyButton buttonWithType:UIButtonTypeCustom];
或者我必须使用alloc
和initWithFrame
代替?
谢谢
对不起,我刚从苹果的文档中找到这个:
这个方法是一个方便的构造函数,用于创建具有特定配置的按钮对象。如果你子类化UIButton,这个方法不会返回你子类的一个实例。如果你想创建一个特定子类的实例,你必须直接分配/初始化按钮。
buttonWithType
是UIButton
的class method
,它返回UIButton
类型,虽然您的MyButton
继承了UIButton
,但它也不能返回MyButton
类型
更多信息你可以看到我之前的回答:this