我嘗試使用此代碼,但我得到錯誤「timeLabel undeclared」定時器在xcode錯誤
我該如何聲明timeLabel。感謝..
在此先感謝...試圖讓定時器工作,但似乎有一些錯誤的代碼..
//In Header
int timeSec = 0;
int timeMin = 0;
NSTimer *timer;
//Call This to Start timer, will tick every second
-(void) StartTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer
{
timeSec++;
if (timeSec == 60)
{
timeSec = 0;
timeMin++;
}
//Format the string 00:00
NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
[timeLabel setStringValue:timeNow];
}
//Call this to stop the timer event(could use as a 'Pause' or 'Reset')
- (void) StopTimer
{
[timer invalidate];
timeSec = 0;
timeMin = 0;
//Since we reset here, and timerTick won't update your label again, we need to refresh it again.
//Format the string in 00:00
NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
[timeLabel setStringValue:timeNow];
}
在.h文件中聲明。 – Ishu 2011-04-25 03:52:10
我已經在.h中聲明插入UILabel * timeLabel並設法解決錯誤,但計時器沒有出現在我的應用程序,我仍然得到警告「UILabel可能不會響應'-setStringValue:'」 – shy 2011-04-25 03:56:55
我設法通過將@property(nonatomic,retain)IBOutlet UILabel *標籤放在警告中; @property(nonatomic,retain)IBOutlet UILabel * timeLabel;但它似乎不工作,我想念的東西 – shy 2011-04-25 04:03:04