Xcoder
Xcoder

Reputation: 45

UILabels crashing my app

So I don't know any way to draw text or strings but a UILabel. So I initialized the label, but it keeps crashing my app.

Here are the methods that initialize the labels and are causing the crash:

-(void) setupScore{

scoreLabel = [NSString stringWithFormat:@"%d", score];
scoreLabel.frame = CGRectMake(262, 250, 100, 40);
[scoreLabel setText: scoreString];

//normally you'll want a transparent background for your label
scoreLabel.backgroundColor = [UIColor clearColor]; 

//you can use non-standard fonts
[scoreLabel setFont:[UIFont fontWithName:@"TimesNewRoman" size: 1.0f]];

//change the label's text color
scoreLabel.textColor = [UIColor whiteColor];

//add it to your view
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self addSubview:scoreLabel]; }

-(void) setupPausedLabel{

pausedLabel = [NSString stringWithFormat:@"Tap To Resume"];
pausedLabel.frame = CGRectMake(262, 250, 100, 40);
[pausedLabel setText: @"Tap To Resume"];

//normally you'll want a transparent background for your label
pausedLabel.backgroundColor = [UIColor clearColor];

//you can use non-standard fonts
[pausedLabel setFont:[UIFont fontWithName:@"TimesNewRoman" size: 1.0f]];

//change the label's text color
pausedLabel.textColor = [UIColor whiteColor];

//add it to your view
pausedLabel.transform = CGAffineTransformMakeRotation(89.53);
[pausedLabel setHidden: YES];
[self addSubview:pausedLabel]; }

Thanks for your help!

Upvotes: 0

Views: 401

Answers (4)

Hardeep Singh
Hardeep Singh

Reputation: 942

-(void) setupScore{

 scoreLabel.text = [NSString stringWithFormat:@"%d", score];
scoreLabel.frame = CGRectMake(262, 250, 100, 40);

[scoreLabel setText: scoreString];

//normally you'll want a transparent background for your label
scoreLabel.backgroundColor = [UIColor clearColor]; 

//you can use non-standard fonts
[scoreLabel setFont:[UIFont fontWithName:@"TimesNewRoman" size: 1.0f]];

//change the label's text color
scoreLabel.textColor = [UIColor whiteColor];

//add it to your view
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self.view addSubview:scoreLabel];

}

It's working fine I'm calling it in viewDidLoad

- (void)viewDidLoad

{ [super viewDidLoad];

scoreLabel=[[UILabel alloc]init];

scoreString=[NSString stringWithString:@"1221"];

score=10;

[self setupScore];

scoreString=[NSString stringWithString:@"11"];


[self setupScore];

scoreString=[NSString stringWithString:@"333"];


[self setupScore];

[self setupScore];

scoreLabel.text=@"okey...";

}

Upvotes: 0

tiguero
tiguero

Reputation: 11537

You are initializing your label with a NSString class it should be a UILabel. Make sure you generate your string with the good formater: %d for integer and %@ for an NSString object. Check this doc to find the appropriate specifier:

NSString* scoreString = [NSString stringWithFormat:@"%d", score];
NSString* anotherString = @"Tap To Resume";
NSString* pausedString = [NSString stringWithFormat:@"%@", anotherString];

Upvotes: 0

Hardeep Singh
Hardeep Singh

Reputation: 942

You are wrong in your first line.

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

scoreLabel=[[UILable alloc]init];
scoreLabel.text=[NSString stringWithFormat:@"%i",score];

And Same Problem in NextMethod

pausedLabel = [NSString stringWithFormat:@"Tap To Resume"];

It's not Right

    pausedLabel=[[UILable alloc]init];
pausedLable.text=[NSString stringWithFormat:@"Tap To Resume"];

UILable text Property you are messing so it's give you crashing problem

Upvotes: 0

Zoleas
Zoleas

Reputation: 4879

You are doing :

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

You should do :

scoreLabel = [[UILabel alloc] init];    // And release it somewhere
scoreString = [NSString stringWithFormat:@"%d", score];

and same for the pauseLabel.

Upvotes: 2

Related Questions