【问题标题】:UILabel not showing correct data because it is nilUILabel 没有显示正确的数据,因为它是 nil
【发布时间】: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


【解决方案1】:

这是一个问得不好的问题,而且代码格式也越来越差,但我会冒险猜测您没有正确连接 nib 文件中的 UILabel 插座。

另外,您的 IBAction“被窃听”的签名有误。

【讨论】:

  • 在IB中完美连接。签名错误是什么意思?
  • 应该是- (IBAction)tapped:(id)sender,而不是- (IBAction)tapped
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
  • 2023-03-16
相关资源
最近更新 更多