таймер в xcode error - PullRequest
       3

таймер в xcode error

0 голосов
/ 25 апреля 2011

я пытаюсь использовать этот код, но я получил ошибку "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];
}

Ответы [ 2 ]

2 голосов
/ 25 апреля 2011
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////
}
0 голосов
/ 25 апреля 2011

Если компилятор говорит, что timeLabel не объявлен, вы, вероятно, забыли объявить его среди переменных экземпляра вашего класса. Посмотрите в своем .h файле и посмотрите, есть ли у вас объявление для timeLabel. Если да, опубликуйте раздел @interface вашего класса, чтобы мы могли видеть, что происходит.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...