iPhone - cannot get int put into UIButton
I am assuming this is just because I am very tired..but for some reason I cannot get my UIBUTTONS to get filled with the numbers that are generated. The numbers are randomly generated except for the answer. I am sure it is something simple. Thanks!
- (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] for开发者_JAVA百科State: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");
}
}
EDIT: Sorry I had accidentally put UILabels, I meant UIButtons.
Arcrandom generates float or double, try cast each to int like this:
[ans2 setTitle:[NSString stringWithFormat:@"%d", ((int)answer)] forState:UIControlEventAllEvents];
If you are using UILabel
. use the setText
instead of setTitle
,
[ans2 settext:[NSString stringWithFormat:@"%d", answer]];
OR
ans2.text = [NSString stringWithFormat:@"%d", answer];
I figured it out, I should not have been waiting for a Control Event in the forState, it should have been this
[ans2 setTitle:[NSString stringWithFormat:@"%d", answer] forState:UIControlStateNormal];
精彩评论