2013-02-27 52 views
-1

我需要幫助。如何在if語句中使用已被稱爲條件的IBAction方法。 XCODE

-(IBAction) equalsPressed:(id)sender{ 



if(((IBAction) dividePressed) = YES){ 
    int equals = labelsNumber.text.intValue; 
    labelsNumber.text = @""; 
    int ans = divide/equals; 
    NSString *answer = [NSString stringWithFormat:@"%d", ans]; 
    labelsNumber.text = answer; 

我正在建立一個計算器,錯誤是「Expected Expression」。

請幫忙

+0

'dividePressed'是什麼?這是一種方法嗎?一個實例變量? – rmaddy 2013-02-27 01:45:37

+0

除了下面的優秀答案,我推薦[這個SO線程](http://stackoverflow.com/q/1643007/620197)以更好地理解'IBAction'是什麼。 – 2013-02-27 01:52:05

回答

0

我想你是在倒退。在equalsPressed方法中,您將進行計算。在dividePressed你存儲操作:

-(IBAction) equalPressed:(id)sender { 
int equals = labelsNumber.text.intValue; 
    int ans; 
    labelsNumber.text = @""; 
    switch(operator) { 
    case DIVIDE: 
    ans = op1/op2; 
    break; 
    } 
    NSString *answer = [NSString stringWithFormat:@"%d", ans]; 
    labelsNumber.text = answer; 
} 

-(IBAction) dividePressed:(id)sender { 
    operator = DIVIDE; 
} 

我會做的操作數(OP1,OP2),它們存儲和equals方法對它們進行操作

1

我假設你同樣的事情正試圖確定是否已經調用了dividePressed。你可以這樣做:

BOOL dividePressed; 

-(IBAction) dividePressed:(id)sender{ 
    dividePressed = YES; 
} 

-(IBAction) equalsPressed:(id)sender{ 
    if(dividePressed) { 

    } 
} 
相關問題