1

So I have setup my UILabel, but when I set the text in my game loop to a string that is the score (so the Text is reset every loop) my app crashes.

This is the error I get:

0x15560b0: cmpl (%eax), %ecx Thread 1

The Breakpoint says this:

EXC_BAD_ACCESS(code=1, address=0x67b30064

Here is how I am setting up my UILabel (in my init method) :

//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];

Thank You!

Jon Reid
  • 20,545
  • 2
  • 64
  • 95
Xcoder
  • 45
  • 1
  • 5

3 Answers3

5

Are you doing your label update off the main queue? I had these crashes until I dispatched the update on the main queue.

dispatch_async(dispatch_get_main_queue(), ^{
    self.lblSyncProgress.text = @"Some string";
});
Sai Ramachandran
  • 393
  • 4
  • 12
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
  • 742
  • 2
  • 7
  • 21
  • 5
    @Coder404, I see this solution is marked as the correct one, but I do not understand how it works, neither what the cause of the problem is. I seem to have the same problem, but I don't know how to adopt your solution because I don't know what it does. Can you please add some information about that? – Daniel S. Apr 22 '14 at 17:15
  • Could someone please explain the underlying cause. – Can Jul 02 '15 at 11:33
-1

I noticed in your code that you had

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

Make sure that "score" is a char. If it is not have a look at this

Community
  • 1
  • 1
Coder404
  • 742
  • 2
  • 7
  • 21