2011-07-29 2 views
2

저는 UILabel입니다. 사용자가 버튼을 누르면 라벨에 값을 더하고 싶습니다. 하지만 이것에 대해 약간의 문제가 있습니다. 여기 내 코드는 다음과 같습니다.UILabel에 표시된 정수에 1을 더합니다.

- (IBAction)addButton2:(id)sender { 
    int integer = 1; 
    integer++; 
    [label1 setText:[NSString stringWithFormat:@"%i",integer]]; 
} 
+0

질문을 수정하셨습니까? 변경 한 내용을 명확하게 나타낼 수 있습니까? 이전 버전이 충돌합니다 – Matt

답변

3

INT는 stringValue에 응답하지 않습니다 [INT stringValue] 늘

-(IBAction)addButton2:(id)sender { 
    static int myInt = 1; 
    myInt++; 
    NSString *string = [NSString stringWithFormat:@"%d", myInt]; 
    [label setText:string]; 
} 
작업
2

정수를 정수에 추가하면 정수는 한 번만 초기화됩니다.

- (IBAction)addButton2:(id)sender 
{ 
    static int integer = 1; 
    integer++; 
    [label1 setText:[NSString stringWithFormat:@"%d", integer]]; 
} 
+0

완료 !!!! 감사 : 디 P.S. 나는 존 트라볼타예요;) – PengOne2

+0

실 거예요, int doesnt stringValue ...? – Matt

+0

미안, 내 편집 참조 =) 나는 조금 피곤하다. –

0

integer을 버튼을 누를 때마다 1로 재설정 한 다음 1 씩 증가시킵니다. 이것은 항상 2이 레이블에 표시되게합니다. 이 기능 이외의 초기화를 이동해야합니다

원래의 질문이 있었다 ...

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    integer = 1; 
    [label1 setText:[integer stringValue]]; 
} 

- (IBAction)addButton2:(id)sender 
{ 
    integer++; 
    [label1 setText:[integer stringValue]]; 
} 
+1

C 세계에서 항상 자동으로 정적으로 만들 것입니다. – Matt

관련 문제