2011-05-14 52 views
2

我認爲這只是因爲我很累..但由於某種原因,我無法讓我的UIBUTTONS充滿了生成的數字。除答案外,這些數字是隨機生成的。我相信這很簡單。謝謝!iPhone - 無法將int放入UIButton

- (void)determineQ { 
    first = arc4random() % 1000; 
    second = arc4random() % 1000; 
    wrong1 = arc4random() % 1000; 
    wrong2 = arc4random() % 1000; 
    wrong3 = arc4random() % 1000; 
    while (first < second) { 
     first = arc4random() % 1000; 
    } 
    [question setText:[NSString stringWithFormat:@"%d - %d = ?", first, second]]; 
    answer = first - second; 
    NSLog(@"%d - %d = %d", first, second, answer); 
    correctLetter = arc4random() % 100; 
    if (correctLetter <= 25) { 
     [ans2 setTitle:[NSString stringWithFormat:@"%d", answer] forState:UIControlEventAllEvents]; 
     [ans2 setTitle:[NSString stringWithFormat:@"%d", wrong1] forState:UIControlEventAllEvents]; 
     [ans3 setTitle:[NSString stringWithFormat:@"%d", wrong2] forState:UIControlEventAllEvents]; 
     [ans4 setTitle:[NSString stringWithFormat:@"%d", wrong3] forState:UIControlEventAllEvents]; 
     NSLog(@"The answer is A"); 
    } else if (correctLetter <= 50) { 
     [ans1 setTitle:[NSString stringWithFormat:@"%d", wrong1] forState:UIControlEventAllEvents]; 
     [ans2 setTitle:[NSString stringWithFormat:@"%d", answer] forState:UIControlEventAllEvents]; 
     [ans3 setTitle:[NSString stringWithFormat:@"%d", wrong2] forState:UIControlEventAllEvents]; 
     [ans4 setTitle:[NSString stringWithFormat:@"%d", wrong3] forState:UIControlEventAllEvents]; 
     NSLog(@"The answer is B"); 
    } else if (correctLetter <= 75) { 
     [ans1 setTitle:[NSString stringWithFormat:@"%d", wrong1] forState:UIControlEventAllEvents]; 
     [ans2 setTitle:[NSString stringWithFormat:@"%d", wrong2] forState:UIControlEventAllEvents]; 
     [ans3 setTitle:[NSString stringWithFormat:@"%d", answer] forState:UIControlEventAllEvents]; 
     [ans4 setTitle:[NSString stringWithFormat:@"%d", wrong3] forState:UIControlEventAllEvents]; 
     NSLog(@"The answer is C"); 
    } else if (correctLetter <= 100) { 
     [ans1 setTitle:[NSString stringWithFormat:@"%d", wrong1] forState:UIControlEventAllEvents]; 
     [ans2 setTitle:[NSString stringWithFormat:@"%d", wrong2] forState:UIControlEventAllEvents]; 
     [ans3 setTitle:[NSString stringWithFormat:@"%d", wrong3] forState:UIControlEventAllEvents]; 
     [ans4 setTitle:[NSString stringWithFormat:@"%d", answer] forState:UIControlEventAllEvents]; 
     NSLog(@"The answer is D"); 
    } 
} 

編輯:對不起,我不小心把UILabels,我的意思是UIButtons。

+0

你會得到什麼錯誤?不是'first'和'second'是一個float,因此應該是'[question setText:[NSString stringWithFormat:@「%。0f - %.0f =?」,first,second]];' – simonbs

+0

第一個和第二個是實際上是整數。 –

回答

0

我想通了,我不應該一直在等待的forState控制事件,它應該是這個

[ans2 setTitle:[NSString stringWithFormat:@"%d", answer] forState:UIControlStateNormal]; 
3

Arcrandom產生浮動或雙,嘗試投各爲int這樣的:

[ans2 setTitle:[NSString stringWithFormat:@"%d", ((int)answer)] forState:UIControlEventAllEvents]; 
0

如果您正在使用UILabel。使用setText代替setTitle

[ans2 settext:[NSString stringWithFormat:@"%d", answer]]; 

OR

ans2.text = [NSString stringWithFormat:@"%d", answer]; 
+0

我的意思是UIButtons,不小心把UILabels:/ –

+0

@Muller:好的,請在發佈問題時小心。 – Jhaliya