UILabels崩溃我的应用程序

问题描述:

所以我不知道任何方式绘制文本或字符串,但UILabel。所以我初始化了这个标签,但它一直让我的应用崩溃。UILabels崩溃我的应用程序

下面是初始化标签,并导致崩溃的方法:

- (无效)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]; } 

- (无效)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]; } 

感谢您的帮助!

+0

的 “崩溃” 的细节_always_重要。这通常不是崩溃,而是_raception_。什么是例外?什么是异常消息抱怨?并且在什么情况下引发异常? – 2012-08-17 16:44:44

你正在做的:

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

你应该这样做:

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

和同为pauseLabel。

+0

你比我快!大声笑 – tiguero 2012-08-17 16:46:36

+0

哈哈,我可能已经看到了这个问题,当它已经发布的时刻^^ – Zoleas 2012-08-17 16:48:15

+0

好吧,我做到了,但现在,该标签绘制,但崩溃时,我的游戏状态从菜单更改为游戏(即分数现在加起来) 。 – Xcoder 2012-08-17 16:55:56

您正在使用NSString类初始化您的标签,它应该是UILabel。确保使用良好的formater生成字符串:%d为整数,%@为NSString对象。检查此doc以找到适当的说明符:

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

您在第一行中有错误。

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

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

而且在NextMethod

同样的问题
pausedLabel = [NSString stringWithFormat:@"Tap To Resume"]; 

这是不对的

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

UILable文本属性你搞乱,所以它给你崩溃问题

+0

我这样做了,现在它在标签文本更改(即分数正在添加)之后崩溃。还有另一种绘制文本和整数的方法吗? – Xcoder 2012-08-17 17:20:35

+0

但它的工作,当我在你的code.You这个改变你写不了pausedLabel.text = @“String”; – 2012-08-17 17:31:09

+0

我修正了每个人都告诉我的代码,但是当分数开始改变时,应用程序崩溃。 – Xcoder 2012-08-17 17:47:24

-(void) setupScore{ 

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

[scoreLabel setText:sco reString];

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

}

它的正常工作,我称它在viewDidLoad中

- (void)viewDidLoad 

{ [超级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]; 

[email protected]"okey..."; 

}

+0

但我没有使用viewDidLoad。 – Xcoder 2012-08-17 17:48:19

+0

你什么时候调用这个方法?试试[scoreString retain];你在哪里设置字符串。在调用方法之前。 – 2012-08-17 18:00:07

+0

我在我的init函数中调用它,然后传递给initWithCoder。 – Xcoder 2012-08-17 18:08:47