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]; }
感谢您的帮助!
答
你正在做的:
scoreLabel = [NSString stringWithFormat:@"%d", score];
你应该这样做:
scoreLabel = [[UILabel alloc] init]; // And release it somewhere
scoreString = [NSString stringWithFormat:@"%d", score];
和同为pauseLabel。
答
您正在使用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文本属性你搞乱,所以它给你崩溃问题
答
-(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...";
}
的 “崩溃” 的细节_always_重要。这通常不是崩溃,而是_raception_。什么是例外?什么是异常消息抱怨?并且在什么情况下引发异常? – 2012-08-17 16:44:44