Iphone 如何正确地使NSTimer无效
我有一个iPhone应用程序,它有一个名为pressTimer的NSTimer,每当有人触摸这个按钮时,它就会自动关闭。问题是他们经常触摸按钮,我希望计时器在他们抬起手指时停止。所以我在.h中声明了pressTimer,并在.m中合成了它,然后在我创建的触地动作中分配并启动了它。我在TouchUpInside操作中使用代码[pressTimer invalidate],该代码应取消计时器,因为它是一个全局变量。它似乎很好地取消了计时器,但当我去实际触发计时器时,程序完全冻结,即使我没有收到任何代码错误或警告。我是个笨蛋,所以请不要太粗鲁:0 更新:控制台日志-谢谢大家Iphone 如何正确地使NSTimer无效,iphone,objective-c,nstimer,invalidation,Iphone,Objective C,Nstimer,Invalidation,我有一个iPhone应用程序,它有一个名为pressTimer的NSTimer,每当有人触摸这个按钮时,它就会自动关闭。问题是他们经常触摸按钮,我希望计时器在他们抬起手指时停止。所以我在.h中声明了pressTimer,并在.m中合成了它,然后在我创建的触地动作中分配并启动了它。我在TouchUpInside操作中使用代码[pressTimer invalidate],该代码应取消计时器,因为它是一个全局变量。它似乎很好地取消了计时器,但当我去实际触发计时器时,程序完全冻结,即使我没有收到任何代
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”.
这是我使计时器无效的方式:
[timer invalidate];
timer = nil;
您可能遇到的问题是,一旦计时器失效,它就会从运行时循环中删除,当有人想要访问它时,您的程序就会崩溃。在使其无效后,只需使其为零即可。这样,如果有人想要发送消息或对其执行任何操作,它会将消息发送到nil。日志显示
addOne
有问题。。。它和计时器有什么关系?伙计们,我把它弄乱了一点,才弄明白。Anauel停止计时器的方法非常有效,但由于某种原因,我的方法名称与计时器的选择器冲突,所以我将它们都更改了。我还将Anauel的代码添加到我用作选择器的方法中。谢谢 我最好添加对有效性标志和计时器对象实例的检查
@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;
-(void) clearKeepAliveTimer {
if(keepAliveTimer && [keepAliveTimer isValid]) {
[keepAliveTimer invalidate];
keepAliveTimer = nil;
} }
谢谢如果我这样做,虽然我的计时器不能再次触发,对吧。我想做的是,如果用户点击按钮3秒钟,它会弹出一个文本字段来编辑某些内容。这个按钮还有另一个用途,如果你只是简单地点击它,那么我想确保计时器在每次点击按钮时都会持续触发,以进行检查。只需在需要时实例化一个新的计时器。Sweet。我这样做了,我对计时器重新初始化没有问题,但是当计时器触发选择器时,它仍然冻结。如果有帮助的话,我可以发布控制台日志。请发布,会有帮助的。谢谢大家,你们可能知道我不是真的proaddOne是一个iAction,它的前两行是[pressTimer invalidate]和pressTimer=nil。这是连接它们的唯一方法。我也没有收到任何错误或警告