2012-07-21 89 views
2

基本上,我有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]; 


} 
+0

我不知道如何在Objective C代碼,但你可以使用switch語句用一個int比較做哪些動作? – charlypu 2012-07-21 20:33:50

回答

0

(也許你應該看看http://codereview.stackexchange.com

的 「不同」 部分始終形式[tempData doSomethingOn:aString];的,這樣你就可以與選擇 「doSomethingOn:」:參數化

-(void)doSomethingOnString:(NSString*)parameter 
       withSelector:(SEL)selector 
       fromKeyboard:(NSString*)name 
{ 
    // The common parts before 

    [tempData performSelector:selector withObject:parameter]; 

    // The common parts after 
} 

-(void)addElement:(NSString *)currentElement FromKeyboard:(NSString *)name { 
    [self doSomethingOnString:currentElement 
       withSelector:@selector(addElement:) 
       fromKeyboard:name]; 
} 

// etc. 

另請參閱:

+0

而不是調用'addElement:'方法,然後調用'doSomething:'方法,我直接調用'[delegate doSomething:button.titleLabel.text withSelector:@selector(addElement :) fromKeyboard:self.name]',但我得到一個無法識別的選擇器錯誤 – Mahir 2012-07-21 22:29:05

+0

沒關係,我意識到'@ selector'不是必需的。忽略以上評論 – Mahir 2012-07-22 11:26:08

相關問題