0

所以我不知道任何方法来绘制文本或字符串,但 UILabel。所以我初始化了标签,但它总是让我的应用程序崩溃。

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

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

谢谢你的帮助!

4

4 回答 4

2

你正在做:

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

你应该做 :

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

和 pauseLabel 一样。

于 2012-08-17T16:45:21.337 回答
0

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

NSString* scoreString = [NSString stringWithFormat:@"%d", score];
NSString* anotherString = @"Tap To Resume";
NSString* pausedString = [NSString stringWithFormat:@"%@", anotherString];
于 2012-08-17T16:45:57.240 回答
0

你在第一行就错了。

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 text 属性你搞砸了所以它给你崩溃的问题

于 2012-08-17T17:06:12.960 回答
0
-(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];

}

它工作正常我在 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];

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

}

于 2012-08-17T17:42:50.697 回答