xcode 后台3秒时间间隔后重复调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22193131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling method repeatedly after 3 seconds time interval in background
提问by Vizllx
I have gone through many sites but still no answer.
我浏览了很多网站,但仍然没有答案。
I have a methodsuppose void xyz()
, which will get called automaticallyfrom a View Controllerafter every 3 seconds.
我有一个方法假设void xyz()
,它将在每 3 秒后从视图控制器自动调用。
I have no idea what to use, do I have to use NSThreador PerformSelector.
我不知道该用什么,我必须使用NSThread还是PerformSelector。
回答by morroko
Call this method from ViewDidLoad method.ViewDidLoad will when your view will be appear in iPhone device or Simulator.
当您的视图出现在 iPhone 设备或模拟器中时,从 ViewDidLoad 方法调用此方法。ViewDidLoad 将调用此方法。
[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(runMethod) userInfo:nil repeats:YES];
-(void)runMethod
{
}
回答by Sunny Shah
Something like this
像这样的东西
-(void)xyz{
[self performSelectorInBackground:@selector(xyz) withObject:nil];
}
- (void)viewDidLoad {
[self performSelector:@selector(xyz) withObject:nil afterDelay:0.3];
}
回答by Himanshu Joshi
Use NSTimer
用 NSTimer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(xyz) userInfo:nil repeats:YES];
回答by Pushparaj
You should use NSTimer as mentioned by @mokujin. Please visit https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html
您应该使用@mokujin 提到的 NSTimer。请访问https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html