简体   繁体   English

计时器可以在iOS 4.2上正常运行,但不能在iOS 5上运行

[英]Timer runs fine on iOS 4.2 but not working on iOS 5

In my app, 在我的应用中

I have a 3 tab, 我有3个标签

On the first tab I have a timer that is started when Start button is pressed. 在第一个选项卡上,我有一个按下“开始”按钮时启动的计时器。

When we come from the another tab, timer is not getting update. 当我们从另一个选项卡进入时,计时器不会更新。

For updating the timer, I use below code: 为了更新计时器,我使用以下代码:

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


 }

My timer is updating fine with Base SDK iOS 4.2 but not working on iOS 5. What could be wrong? 我的计时器可以使用Base SDK iOS 4.2正常更新,但不能在iOS 5上运行。这可能是什么问题?

THis may not be your problem but the selector is incorrect, from the Apple docs: 这可能不是您的问题,但从Apple文档中选择器不正确:

The selector must have the following signature: 选择器必须具有以下签名:

- (void)timerFireMethod:(NSTimer*)theTimer

so your NSTimer call should be (notice the trailing ":" 因此您的NSTimer调用应为(注意尾随的“:”

myticker = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(ShowActicity:) userInfo:nil repeats:YES];

and the target method have the signature: 和目标方法具有签名:

- (void)ShowActicity:(NSTimer*)theTimer

Note, it is Objective-C convention to name methods with an initial lowercase letter, classes with a leading upper case character. 请注意,Objective-C约定以小写字母开头的方法命名方法,以大写字母开头的类命名方法。

In 4.3 project main.m replace with ios5 main.m File. 在4.3项目中,将main.m替换为ios5 main.m文件。 In appdelegate change the 在appdelegate中更改

   @interface SimpleTimerAppAppDelegate : NSObject 

with

  @interface SimpleTimerAppAppDelegate : UIResponder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM