2012-11-26 5 views
1

iOS 계산기를 만들고 있는데 백 스페이스 버튼을 사용하여 약간의 어려움을 겪고 있습니다 (레이블에 표시된 값의 마지막 번호를 삭제하기 위해). 계산기의 백 스페이스 버튼 만들기

내가 다른 질문이 이어

double currentValue = [screenLabel.text doubleValue] 

을 사용하여 라벨에 현재 값을 얻으려면,

(엑스 코드는 "말한다, 나는

-(IBAction)backspacePressed:(id)sender 
{ 
NSMutableString *string = (NSMutableString*)[screenLabel.text]; 

int length = [string length]; 

NSString *temp = [string substringToIndex:length-1] 
; 

[screenLabel.text setText:[NSString stringWithFormat:@"%@",temp]]; 

} 

뭔가를 시도했지만 작동하지 않습니다 setText가 더 이상 사용되지 않음 ","NSString이 setText에 응답하지 않을 수 있으며 "식별자는 IBAction 내부 코드의 첫 번째 줄에이 필요합니다.

및이 코드가 실제로 작동하도록하려면이 코드를 이해할 수 없습니다.

어떻게해야합니까?

답변

3

그것은해야

[screenLabel setText:[NSString stringWithFormat:@"%@",temp]]; 

엑스 코드는 분명 당신이에 setText' method on an있는 NSString where as you should be calling that on a UILabel의 . Your screenLabel.text is retuning an있는 NSString . You should just use screenLabel alone and should call setText`를 호출하려고하는 것을 말한다.

그냥 그와

NSString *string = [screenLabel text]; 

문제는 당신이 screenLabeltext 메소드를 호출하는 목적-C 구문에 따라 정확하지 [screenLabel.text];를 사용하고,이었다, 사용합니다. 어느 쪽이든 당신은 당신이 NSMutableString를 사용할 필요가 있다고 생각 해달라고,이 방법에서는,

NSString *string = screenLabel.text; 

NSString *string = [screenLabel text]; 

또는

을 사용해야합니다. 대신 NSString을 사용할 수 있습니다. 당신의 방법과 같이 쓸 수있다 짧은에서

,

더 문자열이 존재 없을 때 제로를 표시 할 경우, (지금 삭제) 주석 질문으로 당
-(IBAction)backspacePressed:(id)sender 
{ 
    NSString *string = [screenLabel text]; 
    int length = [string length]; 
    NSString *temp = [string substringToIndex:length-1]; 
    [screenLabel setText:temp]; 
} 

, 시도,

-(IBAction)backspacePressed:(id)sender 
{ 
    NSString *string = [screenLabel text]; 
    int length = [string length]; 
    NSString *temp = [string substringToIndex:length-1]; 

    if ([temp length] == 0) { 
    temp = @"0"; 
    } 
    [screenLabel setText:temp]; 
} 
+1

대단히 감사합니다! :) –

+1

또는 setText 메소드를 건너 뛰고 "screenLabel.text = temp;"를 사용 하시겠습니까? – geowar