Reputation: 449
I am just learning objective-c so my problem might be a simple one. I am creating a simple game that has a label used to keep track of the player's score. The game play works fine and there is no problem computing the score. The problem occurs when I set the label's text property to the value stored in my score variable.
score += (int)someFloatValue;
NSString *scoreText = [NSString stringWithFormat:@"%i", score];
scoreLabel.text = scoreText;
In my game loop, I am always setting this text. So even before the score goes past zero, I am constantly setting the text to a value of zero. No problems so far. As soon as the player scores a point, the value in my score variable changes. Still no problem. But, when I update the label's text with the new score, the locations of all my UI elements get reset back to the location initially set in interface builder. No crashes. And the score gets shown properly in the label. Just every UI element gets moved.
Not sure if any of this is helpful, but I think I set up everything right:
h file:
@interface ViewController : UIViewController{
UILabel *scoreLabel;
NSInteger score;
}
@property (nonatomic, retain) IBOutlet UILabel *scoreLabel;
@property (nonatomic) NSInteger score;
m file:
@interface ViewController ()
@end
@synthesize scoreLabel;
@synthesize score;
Upvotes: 0
Views: 224
Reputation: 16134
Be sure to turn off Auto Layout, if you are moving around UI elements in code and not setting up constraints in IB.
Remove autolayout (constraints) in Interface Builder
Also, you could simplify that code like this (although it's fine as is):
scoreLabel.text = [NSString stringWithFormat:@"%i", score];
Upvotes: 2