UILabel not showing correct data because it is nil
I have a uilabel, and it is not displaying the correct text because it is nil. How can I stop the label from being nil? Thanks MKDev
- (IBAction)tapped
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:kTimerOn] == NO) {
originalCountdownTime = 10;
countdownTime = originalCountdownTime;
[timeLeft setTe开发者_JAVA技巧xt:[NSString stringWithFormat:@"%d", countdownTime]];
countdownTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDownOneSecond) userInfo:nil repeats:YES] retain];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kTimerOn];
}
[self setTapAmount:tapAmount];
}
- (void)setTapAmount:(UILabel *)label
{
++numberOfTaps;
NSString *countString = [NSString stringWithFormat:@"%d", numberOfTaps];
[label setText:countString];
NSLog(@"%@", countString);
}
- (void)countDownOneSecond
{
int newTime = --countdownTime;
timeLeft.text = [NSString stringWithFormat:@"%d", newTime];
if (countdownTime == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congratulations" message:[NSString stringWithFormat:@"You Tapped %i times in %i seconds!", numberOfTaps, originalCountdownTime] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"Try Again", @"View Local Leaderboard",nil];
[alert show];
[alert release];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:kTimerOn];
[countdownTimer invalidate];
}
}
That's poorly asked question, and ever more poorly formatted code, but I'm going to hazard the guess that you have not properly connected the UILabel outlet in your nib file.
Also, your IBAction "tapped" has the wrong signature.
精彩评论