在UILabel上设置文本会导致我的应用程序崩溃

时间:2012-08-18 21:08:13

标签: iphone objective-c ios nsstring uilabel

所以我设置了我的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];

谢谢!

3 个答案:

答案 0 :(得分:5)

您是否正在从主队列中更新标签?在我在主队列上发送更新之前,我遇到了这些崩溃。

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

答案 1 :(得分: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;

答案 2 :(得分:-1)

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

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

确保“得分”是一个字符。如果没有查看this