我尝试使用此代码,但我收到错误“timeLabel undeclared”
我如何声明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];
}
答案 0 :(得分:2)
In .h File write
NSTimer *timer
and make it as property
@property(nonatomic,retain)NSTimer*timer
and in .m file
-(void)yourFunction
{
[timer invalidate];
timer = nil;
timer = [NSTimer scheduledTimerWithTimeInterval: 1.5 target: self selector: @selector(yourFunctionYouWantToCall) userInfo: nil repeats: YES];
}
-(void)YourFunctionYouWantToCall
{
[timer invalidate];
timer = nil;
////Your Code her////
}
答案 1 :(得分:0)
如果编译器声明timeLabel
未声明,您可能忘记在类的实例变量中声明它。查看.h文件,看看你是否有timeLabel
的声明。如果是,请发布您的课程'@interface部分,以便我们可以看到正在发生的事情。