如何正确地使 NSTimer 失效

发布于 2024-09-08 01:59:41 字数 3297 浏览 4 评论 0原文

我有一个带有 NSTimer 的 iPhone 应用程序,名为 pressTimer,每当有人触摸此按钮时,它就会关闭。问题是他们经常触摸按钮,我希望计时器在他们抬起手指时停止。因此,我在 .h 中声明了 pressTimer 并在 .m 中合成了它,然后分配并在我创建的 TouchDown 操作上启动它。我在 TouchUpInside 操作上使用代码 [pressTimer invalidate] 来取消计时器,因为它是一个全局变量。它似乎可以很好地取消计时器,但是当我实际触发计时器时,程序完全冻结,即使我没有收到代码错误或警告。我是菜鸟,所以请不要太粗暴:0

更新:控制台日志 - 谢谢大家!

continue
2010-07-01 16:39:30.981 Clickkr[1573:307] -[__NSCFData invalidate]: unrecognized selector sent to instance 0x154b40
2010-07-01 16:39:30.999 Clickkr[1573:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData invalidate]: unrecognized selector sent to instance 0x154b40'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x309a5fd3 __exceptionPreprocess + 114
    1   libobjc.A.dylib                     0x318b58a5 objc_exception_throw + 24
    2   CoreFoundation                      0x309a9a77 -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
    3   CoreFoundation                      0x309a8f15 ___forwarding___ + 508
    4   CoreFoundation                      0x3093b680 _CF_forwarding_prep_0 + 48
    5   Clickkr                             0x00002b23 -[MyViewController addOne:] + 42
    6   CoreFoundation                      0x309307ad -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
    7   UIKit                               0x31e1f829 -[UIApplication sendAction:to:from:forEvent:] + 84
    8   UIKit                               0x31e1f7c9 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
    9   UIKit                               0x31e1f79b -[UIControl sendAction:to:forEvent:] + 38
    10  UIKit                               0x31e1f4ed -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
    11  UIKit                               0x31e1fb3b -[UIControl touchesEnded:withEvent:] + 342
    12  UIKit                               0x31e1e4ed -[UIWindow _sendTouchesForEvent:] + 368
    13  UIKit                               0x31e1de67 -[UIWindow sendEvent:] + 262
    14  UIKit                               0x31e19b5b -[UIApplication sendEvent:] + 298
    15  UIKit                               0x31e19507 _UIApplicationHandleEvent + 5022
    16  GraphicsServices                    0x30a8c147 PurpleEventCallback + 666
    17  CoreFoundation                      0x3097baab __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
    18  CoreFoundation                      0x3097d84f __CFRunLoopDoSource1 + 166
    19  CoreFoundation                      0x3097e62d __CFRunLoopRun + 520
    20  CoreFoundation                      0x309278eb CFRunLoopRunSpecific + 230
    21  CoreFoundation                      0x309277f3 CFRunLoopRunInMode + 58
    22  GraphicsServices                    0x30a8b6ef GSEventRunModal + 114
    23  GraphicsServices                    0x30a8b79b GSEventRun + 62
    24  UIKit                               0x31dc32a7 -[UIApplication _run] + 402
    25  UIKit                               0x31dc1e17 UIApplicationMain + 670
    26  Clickkr                             0x00002333 main + 70
    27  Clickkr                             0x000022e8 start + 40
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.

I've got an iPhone app with an NSTimer called pressTimer that goes off every time someone touches this button. The problem is they touch the button a lot, and I want the timer to stop when they lift up their finger. So I declared pressTimer in my .h and synthesized it in .m then assigned and started it on the TouchDown action I created. I use the code [pressTimer invalidate] on my TouchUpInside action that should cancel the timer, since it's a global variable. It seems to cancel the timer fine, but then when I go to actually trip the timer, the program freezes up completely, even though I get no code errors or warnings. I'm a noob, so please don't be too rough :0

Update: Console logs- Thanks guys!

continue
2010-07-01 16:39:30.981 Clickkr[1573:307] -[__NSCFData invalidate]: unrecognized selector sent to instance 0x154b40
2010-07-01 16:39:30.999 Clickkr[1573:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData invalidate]: unrecognized selector sent to instance 0x154b40'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x309a5fd3 __exceptionPreprocess + 114
    1   libobjc.A.dylib                     0x318b58a5 objc_exception_throw + 24
    2   CoreFoundation                      0x309a9a77 -[NSObject(NSObject) doesNotRecognizeSelector:] + 102
    3   CoreFoundation                      0x309a8f15 ___forwarding___ + 508
    4   CoreFoundation                      0x3093b680 _CF_forwarding_prep_0 + 48
    5   Clickkr                             0x00002b23 -[MyViewController addOne:] + 42
    6   CoreFoundation                      0x309307ad -[NSObject(NSObject) performSelector:withObject:withObject:] + 24
    7   UIKit                               0x31e1f829 -[UIApplication sendAction:to:from:forEvent:] + 84
    8   UIKit                               0x31e1f7c9 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
    9   UIKit                               0x31e1f79b -[UIControl sendAction:to:forEvent:] + 38
    10  UIKit                               0x31e1f4ed -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 356
    11  UIKit                               0x31e1fb3b -[UIControl touchesEnded:withEvent:] + 342
    12  UIKit                               0x31e1e4ed -[UIWindow _sendTouchesForEvent:] + 368
    13  UIKit                               0x31e1de67 -[UIWindow sendEvent:] + 262
    14  UIKit                               0x31e19b5b -[UIApplication sendEvent:] + 298
    15  UIKit                               0x31e19507 _UIApplicationHandleEvent + 5022
    16  GraphicsServices                    0x30a8c147 PurpleEventCallback + 666
    17  CoreFoundation                      0x3097baab __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26
    18  CoreFoundation                      0x3097d84f __CFRunLoopDoSource1 + 166
    19  CoreFoundation                      0x3097e62d __CFRunLoopRun + 520
    20  CoreFoundation                      0x309278eb CFRunLoopRunSpecific + 230
    21  CoreFoundation                      0x309277f3 CFRunLoopRunInMode + 58
    22  GraphicsServices                    0x30a8b6ef GSEventRunModal + 114
    23  GraphicsServices                    0x30a8b79b GSEventRun + 62
    24  UIKit                               0x31dc32a7 -[UIApplication _run] + 402
    25  UIKit                               0x31dc1e17 UIApplicationMain + 670
    26  Clickkr                             0x00002333 main + 70
    27  Clickkr                             0x000022e8 start + 40
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

贪了杯 2024-09-15 01:59:41

这就是我使计时器无效的方式:

[timer invalidate];
timer = nil;

您可能遇到的问题是,一旦计时器无效,它就会从运行时循环中删除,当有人想要访问它时,您的程序就会崩溃。使其无效后,使其为零即可。这样,如果有人想发送消息或对其执行任何操作,它会将其发送为零。

This is the way I invalidate timers:

[timer invalidate];
timer = nil;

The problem you're probably having is that once your timer is invalidated, it is removed from the runtime loop and when someone wants to access it, your program crashes. After invalidating it, just make it so that it is nil. That way if someone wants to send a message or do anything to it, it'll send it to nil.

○愚か者の日 2024-09-15 01:59:41
@interface
  NSTimer * yourTimer;

@property (nonatomic, retain) yourTimer;
@synthesize yourTimer;

@implement    
  self.yourTimer = [NSTimer scheduledTimerWithTimeInterval:5
                                                    target:self
                                                  selector:@selector(yourSelector)
                                                  userInfo:nil
                                                   repeats:NO];

  //Wherever you want to stop the timer,
  [yourTimer invalidate];
  self.yourTimer = nil;
@interface
  NSTimer * yourTimer;

@property (nonatomic, retain) yourTimer;
@synthesize yourTimer;

@implement    
  self.yourTimer = [NSTimer scheduledTimerWithTimeInterval:5
                                                    target:self
                                                  selector:@selector(yourSelector)
                                                  userInfo:nil
                                                   repeats:NO];

  //Wherever you want to stop the timer,
  [yourTimer invalidate];
  self.yourTimer = nil;
别靠近我心 2024-09-15 01:59:41

日志显示 addOne 有问题...它与计时器有何关系?

The log tells something wrong with addOne ... how's it related to the timer?

听风念你 2024-09-15 01:59:41

伙计们,我在稍微搞乱之后就弄清楚了。 Anauel 停止计时器的方法效果很好,但由于某种原因,我的方法名称与计时器的选择器发生冲突,所以我更改了它们。我还将 Anauel 的代码添加到我用作选择器的方法中。谢谢!

Guys I figured it out after a little bit of messing with it. Anauel's method for stopping timers worked great, but for some reason my method name was clashing with the timer's selector, so I changed them both. I also added Anauel's code to the method I used as a selector. Thanks!

淡淡の花香 2024-09-15 01:59:41

我最好添加对有效性标志和计时器对象实例的检查

-(void) clearKeepAliveTimer {
    if(keepAliveTimer && [keepAliveTimer isValid]) {
        [keepAliveTimer invalidate];
        keepAliveTimer = nil;
    } }

I would better add a check of the validity flag and timer object instance

-(void) clearKeepAliveTimer {
    if(keepAliveTimer && [keepAliveTimer isValid]) {
        [keepAliveTimer invalidate];
        keepAliveTimer = nil;
    } }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文