[英]Timer runs fine on iOS 4.2 but not working on iOS 5
在我的應用中
我有3個標簽
在第一個選項卡上,我有一個按下“開始”按鈕時啟動的計時器。
當我們從另一個選項卡進入時,計時器不會更新。
為了更新計時器,我使用以下代碼:
myticker = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(ShowActicity) userInfo:nil repeats:YES];
-(void)ShowActicity
{
Here I have the ticks,minute,hour values
ticks =ticks + 1;
if(ticks > 59)
{
minute = minute + 1;
ticks = 0;
if(minute > 59)
{
hour = hour + 1;
minute = 0;
if(hour > 23)
{
hour = 0;
}
}
}
[lbl_timer setText:[NSString stringWithFormat:@"%02d:%02d:%02d",hour,minute,ticks]];
}
我的計時器可以使用Base SDK iOS 4.2正常更新,但不能在iOS 5上運行。這可能是什么問題?
這可能不是您的問題,但從Apple文檔中選擇器不正確:
選擇器必須具有以下簽名:
- (void)timerFireMethod:(NSTimer*)theTimer
因此您的NSTimer調用應為(注意尾隨的“:”
myticker = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(ShowActicity:) userInfo:nil repeats:YES];
和目標方法具有簽名:
- (void)ShowActicity:(NSTimer*)theTimer
請注意,Objective-C約定以小寫字母開頭的方法命名方法,以大寫字母開頭的類命名方法。
在4.3項目中,將main.m替換為ios5 main.m文件。 在appdelegate中更改
@interface SimpleTimerAppAppDelegate : NSObject
與
@interface SimpleTimerAppAppDelegate : UIResponder
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.