基本上,我有 3 个委托方法,它们具有几乎完全相同的代码,除了每个方法和参数中的 1 行。我意识到我可以封装很多代码以在每个方法中制作 3 或 4 行代码,但我想知道是否有一种方法可以只创建 1 个方法。
此外,被调用tempData
的方法与下面的委托方法具有相同的方法名称,但它们实际上是不同的方法。
- (void)addElement:(NSString *)currentElement FromKeyboard:(NSString *)name {
UIView *tempView;
NSMutableArray *tempViewList;
EquationData *tempData;
if ([name isEqualToString:@"leftEqCellKeyboard"]) {
tempData = leftData;
tempViewList = leftEqViewList;
tempView = equationCell.leftView;
}
if ([name isEqualToString:@"rightEqCellKeyboard"]) {
tempData = rightData;
tempViewList = rightEqViewList;
tempView = equationCell.rightView;
}
[tempData addElement:currentElement]; // different
if ([tempViewList count] != 0)
[self clearViewsStoredIn:tempViewList];
[self setUpView:tempView fromArray:tempData.equation toArray:tempViewList];
}
- (void)changeState:(NSString *)stateName FromKeyboard:(NSString *)name {
UIView *tempView;
NSMutableArray *tempViewList;
EquationData *tempData;
if ([name isEqualToString:@"leftEqCellKeyboard"]) {
tempData = leftData;
tempViewList = leftEqViewList;
tempView = equationCell.leftView;
}
if ([name isEqualToString:@"rightEqCellKeyboard"]) {
tempData = rightData;
tempViewList = rightEqViewList;
tempView = equationCell.rightView;
}
[tempData changeState:stateName]; // different
if ([tempViewList count] != 0)
[self clearViewsStoredIn:tempViewList];
[self setUpView:tempView fromArray:tempData.equation toArray:tempViewList];
}
- (void)changeCharge:(NSString *)chargeIncrease FromKeyboard:(NSString *)name {
UIView *tempView;
NSMutableArray *tempViewList;
EquationData *tempData;
if ([name isEqualToString:@"leftEqCellKeyboard"]) {
tempData = leftData;
tempViewList = leftEqViewList;
tempView = equationCell.leftView;
}
if ([name isEqualToString:@"rightEqCellKeyboard"]) {
tempData = rightData;
tempViewList = rightEqViewList;
tempView = equationCell.rightView;
}
[tempData changeCharge:chargeIncrease]; // different
if ([tempViewList count] != 0)
[self clearViewsStoredIn:tempViewList];
[self setUpView:tempView fromArray:tempData.equation toArray:tempViewList];
}