简体   繁体   English

NSTimer ScheduledTimerWithTimeInterval:target:selector:userInfo:repeats不调用该方法

[英]NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats doesn't invoke the method

The timer never invokes the method. 计时器从不调用该方法。 What am I doing wrong ? 我究竟做错了什么 ? This is the code: 这是代码:

NSTimer *manualOverlayTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(hideManual) userInfo:nil repeats:NO];

method: 方法:

-(void)hideManual

thanks 谢谢

It was a thread issue. 这是一个线程问题。 I've fixed with: 我已经解决了:

dispatch_async(dispatch_get_main_queue(), ^{
    // Timer here
});

You don't need an NSTimer for a task of this sort. 您不需要NSTimer即可完成此类任务。 To hide your view object after a specific period of time on main thread you can use a gcd method dispatch_after 要在特定时间段后在主线程上隐藏视图对象,可以使用gcd方法dispatch_after

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 2.0), dispatch_get_main_queue(), ^(void){
    // Your code
  });

where 2.0 is an amount of seconds that will pass before the block will get executed 其中2.0是执行块之前将经过的秒数

Just use this 只是用这个

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(hideManual) userInfo:nil repeats:NO];

EDIT 编辑

This code work well when btn (an UIButton ) pressed and -(void)btnPressed function called. 按下btn (一个UIButton )并调用-(void)btnPressed函数时,此代码运行良好。

-(void)btnPressed{
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(hideManual) userInfo:nil repeats:NO];
}

-(void)hideManual{
    NSLog(@"Hi, I'm here!");
}

try this : 尝试这个 :

NSTimer *manualOverlayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                              target:self
                              selector:@selector(hideManual)
                              userInfo:nil
                              repeats:YES];

repeat:YES instead of [[NSRunLoop currentRunLoop] addTimer:manualOverlayTimer forMode:NSDefaultRunLoopMode];

in your code fragments it does not show when and where the NSTimer is fired. 在您的代码片段中,它不会显示触发NSTimer时间和位置。

if you want to use the NSTimer , you should fire the timer after the init, like this: 如果要使用NSTimer ,则应在初始化之后触发计时器,如下所示:

NSTimer *_timer = [NSTimer scheduledTimerWithTimeInterval:2.f target:self selector:@selector(hideManual) userInfo:nil repeats:false];
[_timer fire];

but you can use the following line in your any method as well: 但您也可以在任何方法中使用以下行:

[self performSelector:@selector(hideManual) withObject:nil afterDelay:2.f];

it looks easier in this case than working with NSTimer . 在这种情况下,它看起来比使用NSTimer容易。

暂无
暂无

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

相关问题 如何在'scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:'中将多个参数传递给选择器 - How do I pass more than one parameter to the selector in 'scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:' 这是什么错误“ + [NSTimer ScheduledTimerWithInterval:target:selector:userInfo:repeats:]:无法识别的选择器已发送到类0x1c864a0'”? - What this error is “+[NSTimer scheduledTimerWithInterval:target:selector:userInfo:repeats:]: unrecognized selector sent to class 0x1c864a0' ”? iOS 9兼容版NSTimer scheduledTimerWithTimeInterval:重复:阻止:? - iOS 9 compatible version of NSTimer scheduledTimerWithTimeInterval:repeats:block:? 我可以使用NSTimer的ScheduledTimerWithTimeInterval方法1天(24小时)是否重复? - Can I use NSTimer's scheduledTimerWithTimeInterval method for 1 day (24 hour) with or without repeats? 无法识别的选择器已发送到NSTimer ScheduledTimerWithTimeInterval中的实例 - unrecognized selector sent to instance in NSTimer scheduledTimerWithTimeInterval NSTimer目标和userInfo参数问题 - NSTimer target and userInfo parameter questions NSTimer不会将参数传递给选择器 - NSTimer doesn't pass arguments to the selector NSTimer不会调用他的选择器 - NSTimer doesn't call his selector NSTimer不调用方法 - NSTimer doesn't call method 没有带有计时器的选择器'scheduledTimerWithTimeInterval的已知类方法 - no known class method for selector 'scheduledTimerWithTimeInterval with timer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM