【发布时间】:2011-06-05 11:33:54
【问题描述】:
我有一个 uilabel,但它没有显示正确的文本,因为它是 nil。我怎样才能阻止标签为零?谢谢 MKDev
- (IBAction)tapped
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:kTimerOn] == NO) {
originalCountdownTime = 10;
countdownTime = originalCountdownTime;
[timeLeft setText:[NSString stringWithFormat:@"%d", countdownTime]];
countdownTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDownOneSecond) userInfo:nil repeats:YES] retain];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kTimerOn];
}
[self setTapAmount:tapAmount];
}
- (void)setTapAmount:(UILabel *)label
{
++numberOfTaps;
NSString *countString = [NSString stringWithFormat:@"%d", numberOfTaps];
[label setText:countString];
NSLog(@"%@", countString);
}
- (void)countDownOneSecond
{
int newTime = --countdownTime;
timeLeft.text = [NSString stringWithFormat:@"%d", newTime];
if (countdownTime == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congratulations" message:[NSString stringWithFormat:@"You Tapped %i times in %i seconds!", numberOfTaps, originalCountdownTime] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"Try Again", @"View Local Leaderboard",nil];
[alert show];
[alert release];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:kTimerOn];
[countdownTimer invalidate];
}
}
【问题讨论】:
-
"我怎样才能阻止标签为 nil?"一定是我今天看到的最好的问题!
-
我编辑了您的问题以正确格式化您的代码。与其他网站不同,您不会在此处将代码包装在
标记中。该站点将左侧四个空格插入的文本块识别为代码块。编辑器上有一个“代码”按钮,可以一次性将选定范围的文本设置为代码。
标签: objective-c ios4 uilabel int