2011-04-25 53 views
0

我嘗試使用此代碼,但我得到錯誤「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]; 
} 
+0

在.h文件中聲明。 – Ishu 2011-04-25 03:52:10

+0

我已經在.h中聲明插入UILabel * timeLabel並設法解決錯誤,但計時器沒有出現在我的應用程序,我仍然得到警告「UILabel可能不會響應'-setStringValue:'」 – shy 2011-04-25 03:56:55

+0

我設法通過將@property(nonatomic,retain)IBOutlet UILabel *標籤放在警告中; @property(nonatomic,retain)IBOutlet UILabel * timeLabel;但它似乎不工作,我想念的東西 – shy 2011-04-25 04:03:04

回答

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

如果編譯器說timeLabel是未申報的,你可能已經遺忘在類的實例變量中聲明它。查看你的.h文件,看看你是否有一個timeLabel的聲明。如果是的話,請發佈您的班級'@interface部分,以便我們可以看到發生了什麼。

+0

@interface AnimateViewController:UIViewController { \t \t UIImageView * imageView; \t UILabel * label; \t UILabel * timeLabel; } @property(nonatomic,retain)IBOutlet UIImageView * imageView; @property(nonatomic,retain)IBOutlet UILabel * label; @property(nonatomic,retain)IBOutlet UILabel * timeLabel;對不起,我仍然新編程 – shy 2011-04-25 04:00:34

+0

看看IBOutlets是否正確連接 – visakh7 2011-04-25 04:31:20