题
我的iPhone应用程序具有抽象的类WorkingView。它是Uiview的子类。工作视图反过来将有一堆混凝土子类。其中两个将是简化和乘法浏览。
我正在尝试编写创建新实例的方法。此方法应将传递模式传递并返回适当的混凝土子类的实例。根据答案 这个问题, ,这就是我到目前为止所拥有的:
+ (id)newWorkingViewWithFrame:(CGRect)frame mode: (modeEnum) mode {
WorkingView *ret;
switch (mode) {
case simplifyMode:
ret = [[SimplifyView alloc]initWithFrame: frame];
break;
case multiplyMode:
ret = [[MultiplyView alloc]initWithFrame: frame];
break;
// additional cases here
default:
return nil;
}
// more initialization here
return ret;
}
到现在为止还挺好。但这是问题。在SimplifyView的Init方法中,我需要通过Uiview的Init方法运行对象。调用[Super InitwithFrame:Frame]让我只能使用WorkingView,而不是一直到Uiview。我想我可以创建一个initwithframe:workingview中的方法,然后又调用uiview的initwithframe:method-但是感觉到了hackish。
处理此问题的合适方法是什么?
解决方案
如果您打电话 [super initWithFrame:frame]
从您的子类中,运行时将攀登继承层次结构,以寻找从Super开始实现该方法的类。如果超级无法实现 initWithFrame:
, ,运行时将继续攀登,直到最终找到并在UIVIEW中调用默认实现。
不隶属于 StackOverflow