一直致力于单视图游戏,其中包括targetValue和score,两者都应出现在视图的出口中。将它们连接到IB中的视图控制器(文件所有者),并且targetValue曾经出现,但现在,在添加一些代码后,它不再出现。
怎么了?
以下是BullseyeViewController.m的代码:
#import "BullsEyeViewController.h"
@implementation BullsEyeViewController {
int currentValue;
int targetValue;
int score;
}
@synthesize slider;
@synthesize targetLabel;
@synthesize scoreLabel;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)updateLabels
{
self.targetLabel.text = [NSString stringWithFormat:@"%d", targetValue];
self.scoreLabel.text = [NSString stringWithFormat:@"%d",score];
}
- (void)startNewRound
{
targetValue = 1 + (arc4random() % 100);
currentValue = self.slider.value;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self startNewRound];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.slider = nil;
self.targetLabel = nil;
self.scoreLabel = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
- (IBAction)showAlert
{
int difference = abs(targetValue - currentValue);
int points = 100 - difference;
score += points;
NSString *title;
if (difference == 0) {
title = @"Perfect!";
points += 100;
} else if (difference < 5) {
if (difference == 1) {
points += 50;
}
title = @"You almost had it!";
} else if (difference < 10) {
title = @"Pretty good!";
} else {
title = @"Not even close...";
}
NSString *message = [NSString stringWithFormat:@"You scored %d points", points];
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:title
message:message
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alertView show];
}
- (IBAction)sliderMoved:(UISlider *)slider
{
currentValue = lroundf(slider.value);
}
@end
答案 0 :(得分:0)
如果你在updateLabels上设置一个断点,它是否会命中?
另外,将updateLabels
方法更改为:
- (void)updateLabels
{
targetLabel.text = [NSString stringWithFormat:@"%d", targetValue];
scoreLabel.text = [NSString stringWithFormat:@"%d",score];
}
您是否从视图控制器外部引用这些标签?如果没有,他们是否需要成为财产?