开发者

timer in xcode error

开发者 https://www.devze.com 2023-02-28 17:13 出处:网络
开发者_开发知识库i try to use this code but i got error \"timeLabel undeclared\" how do i declare the timeLabel. thanks..

开发者_开发知识库i try to use this code but i got error "timeLabel undeclared"

how do i declare the timeLabel. thanks..

thanks in advance... try to get the timer working but seem there some error with the code..

//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];
}


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////
}


If the compiler says that timeLabel is undeclared, you've probably forgotten to declare it among your class' instance variables. Look in your .h file and see if you've got a declaration for timeLabel. If yes, post your class' @interface section so we can see what's going on.

0

精彩评论

暂无评论...
验证码 换一张
取 消