NSTimer 导致“无法识别的选择器"开火时崩溃 [英] NSTimer causes "unrecognized selector" crash when it fires

查看:66
本文介绍了NSTimer 导致“无法识别的选择器"开火时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NSTimer 来运行动画(现在只需将其称为 myMethod).但是,它会导致崩溃.

I'm using a NSTimer to run an animation (for now just call it myMethod). However, its causing a crash.

代码如下:

@implementation SecondViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.


- (void) myMethod
{
    NSLog(@"Mark Timer Fire");

}


- (void)viewDidLoad
{ 
[super viewDidLoad];



NSLog(@"We've loaded scan");

[NSTimer scheduledTimerWithTimeInterval:2.0
                                 target:self
                               selector:@selector(myMethod:)
                               userInfo:nil
                                repeats:YES];

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


}

这是崩溃期间的输出

-[SecondViewController myMethod:]: 无法识别的选择器发送到实例 0x4b2ca402012-06-21 12:19:53.297 Lie Detector [38912:207] * 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:-[SecondViewController myMethod:]:无法识别的选择器发送到实例0x4b2ca40'

-[SecondViewController myMethod:]: unrecognized selector sent to instance 0x4b2ca40 2012-06-21 12:19:53.297 Lie Detector[38912:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SecondViewController myMethod:]: unrecognized selector sent to instance 0x4b2ca40'

那么我在这里做错了什么?

So what am I doing wrong here?

推荐答案

要么只能使用

- (void)myMethod: (id)sender
{
 // Do things
}

或者你可以这样做(从两个方法名称中删除 :)..

or you can do (remove : from both the method name)..

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

希望对你有帮助

这篇关于NSTimer 导致“无法识别的选择器"开火时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆