【问题标题】:Timer runs fine on iOS 4.2 but not working on iOS 5计时器在 iOS 4.2 上运行良好,但在 iOS 5 上无法运行
【发布时间】:2012-01-06 08:41:26
【问题描述】:

在我的应用中,

我有一个 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 上无法正常工作。 有什么问题?

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios4 ios5


    【解决方案1】:

    这可能不是您的问题,但选择器不正确,来自 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 约定以小写开头的方法命名方法,以大写字符开头的类。

    【讨论】:

    • 感谢您的回答,我已尝试将方法 -(void)ShowActicity 更改为 - (void)ShowActicity:(NSTimer*)theTimer,但它不起作用。
    【解决方案2】:

    在 4.3 项目 main.m 替换为 ios5 main.m 文件。在 appdelegate 中更改

       @interface SimpleTimerAppAppDelegate : NSObject 
    

      @interface SimpleTimerAppAppDelegate : UIResponder
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多