【问题标题】:Setting the text on a UILabel is crashing my app在 UILabel 上设置文本会使我的应用程序崩溃
【发布时间】:2012-08-14 20:59:55
【问题描述】:

所以我已经设置了我的 UILabel,但是当我将游戏循环中的文本设置为作为分数的字符串时(因此文本在每个循环中重置),我的应用程序崩溃了。

这是我得到的错误:

0x15560b0: cmpl (%eax), %ecx 线程 1

断点是这样说的:

EXC_BAD_ACCESS(代码=1,地址=0x67b30064

这是我设置 UILabel 的方式(在我的 init 方法中):

//SET UP THE SCORE LABEL HERE
scoreLabel = [[UILabel alloc] init];
scoreString = [NSString stringWithFormat:@"%d", score];
[scoreLabel setFont: [UIFont fontWithName: @"TimesNewRoman" size: 10.0f]];
[scoreLabel setFrame: CGRectMake(262, 250, 100, 40)];
[scoreLabel setBackgroundColor:[UIColor clearColor]];
[scoreLabel setTextColor: [UIColor clearColor]];
[scoreLabel setTextColor: [UIColor whiteColor]];
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self addSubview: scoreLabel];
//[scoreLabel setText: scoreString];

谢谢!

【问题讨论】:

  • 您是否在 .h 文件中定义了 UILabel ?

标签: iphone objective-c ios nsstring uilabel


【解决方案1】:

您是否在主队列之外进行标签更新?在我将更新发送到主队列之前,我遇到了这些崩溃。

dispatch_async(dispatch_get_main_queue(), ^{
    self.lblSyncProgress.text = @"Some string";
});

【讨论】:

    【解决方案2】:
    //in your .h file
    UILabel *scoreLabel;
    NSString *scoreString;
    
    
    //in your .m file    
    //SET UP THE SCORE LABEL HERE 
    scoreLabel = [[UILabel alloc] init];
     [scoreLabel setFont: [UIFont fontWithName: @"TimesNewRoman" size: 10.0f]];
     [scoreLabel setFrame: CGRectMake(262, 250, 100, 40)];
     [scoreLabel setBackgroundColor:[UIColor clearColor]];
     [scoreLabel setTextColor: [UIColor clearColor]];
     [scoreLabel setTextColor: [UIColor whiteColor]];
     scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
     [self addSubview: scoreLabel]; 
    
    
    //In your update method
     scoreString = [NSString stringWithFormat:@"%d", score];
    scoreLabel.text = scoreString;
    

    【讨论】:

    • @Coder404,我看到这个解决方案被标记为正确的,但我不明白它是如何工作的,也不知道问题的原因是什么。我似乎有同样的问题,但我不知道如何采用您的解决方案,因为我不知道它的作用。能否请您补充一些相关信息?
    • 有人能解释一下根本原因吗。
    【解决方案3】:

    我在你的代码中注意到你有

    scoreString = [NSString stringWithFormat:@"%d", score];
    

    确保“score”是一个字符。如果不是请看this

    【讨论】:

    • 有没有办法用 char 记录分数?
    • 是的,但值“score”必须在您的 .h 文件中定义为字符
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多