UILabel 未显示正确的数据,因为它为零

发布于 2024-10-11 12:50:13 字数 1454 浏览 6 评论 0原文

我有一个 uilabel,它没有显示正确的文本,因为它为零。我怎样才能阻止标签为零?谢谢 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];
    }
}

I have a uilabel, and it is not displaying the correct text because it is nil. How can I stop the label from being nil? Thanks
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];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-10-18 12:50:13

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

另外,您“点击”的 IBAction 签名错误。

That's poorly asked question, and ever more poorly formatted code, but I'm going to hazard the guess that you have not properly connected the UILabel outlet in your nib file.

Also, your IBAction "tapped" has the wrong signature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文