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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:43:07  来源:igfitidea点击:

calling method repeatedly after 3 seconds time interval in background

iosiphoneobjective-cxcodensthread

提问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];