有几种方法可以处理这个问题。对现有代码的最小影响将是将方法区分为一般应用于UIViewControllers的方法和那些特别适用于子类的方法。调用被声明为特定子类的堆栈变量的子类方法…
for (NSUInteger loop = 0; loop < [switchLabels count]; loop++) {
// Create the view controller object.
UIViewController *vc;
// Create the custom switch view.
if (loop < 3) {
CustomSwitchView *screen = [[CustomSwitchView alloc] initWithNibName:@"CustomSwitchView" bundle:nil];
[screen setPassedInType:switchTypes[loop]];
[screen setDelegate:self];
[screen setTitles:switchTitles[loop] state:[switchSettings[loop] boolValue]];
vc = screen;
} else {
CustomTripleSwitchView *screen = [[CustomTripleSwitchView alloc] initWithNibName:@"CustomTripleSwitchView" bundle:nil];
[screen setPassedInType:switchTypes[loop]];
[screen setDelegate:self];
[screen setTitles:switchTitles[loop] state:[switchSettings[loop] boolValue]];
vc = screen;
}
// Create the custom switch view.
[self addChildViewController:vc];
[vc.view setFrame:CGRectMake((self.view.frame.size.width - 150), ((UILabel *)switchLabels[loop]).frame.origin.y, 144, 72)];
[scrollTopView addSubview:vc.view];
[vc didMoveToParentViewController:self];
}
如果我们在项目中遇到这个问题,这是一个好的解决方案。当你看到这类事情激增时,是时候开始思考:(a)我应该在每个类上定义一个协议(作为一个适当的建议者),还是(b)这些真正的子类彼此相关,比如
CustomTripleSwitchView
真的是
CustomSwitchView
是吗?