基本上,我有3個委託方法幾乎完全相同的代碼,除了在每一行和參數。我意識到我可以封裝很多代碼,以在每種方法中製作3或4行代碼,但我想知道是否有創建1種方法的方法。我有沒有辦法將這3種方法合併爲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];
}
我不知道如何在Objective C代碼,但你可以使用switch語句用一個int比較做哪些動作? – charlypu 2012-07-21 20:33:50