ios 延迟后执行选择器只调用一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16142366/
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-08-30 23:17:09  来源:igfitidea点击:

perform selector after delay only called once

iosobjective-cxcodeviewdidloadperformselector

提问by user2268539

I have an app in which i need to call an instance method after every 1 or 2 seconds. Now if i place

我有一个应用程序,我需要在每 1 或 2 秒后调用一个实例方法。现在如果我放置

[self performSelector:@selector(getMatchListWS) withObject:nil afterDelay:1.0];

in viewDidLoad: or viewWillAppear: , the method getMatchListWS is called only once as the view appears or loads. But i need to call the method continuously even when the user is on that view without the view being disappeared or unload. So, what is the correct place or delegate method in which i can add performSelector method so that it is called every second without having to unload the view again and again. Do i need to do something in background or on main thread.? Thanks in advance!!

在 viewDidLoad: 或 viewWillAppear: 中,方法 getMatchListWS 在视图出现或加载时只调用一次。但是即使用户在该视图上而视图没有消失或卸载,我也需要连续调用该方法。那么,我可以在其中添加 performSelector 方法以便每秒调用它而不必一次又一次地卸载视图的正确位置或委托方法是什么。我需要在后台或主线程上做些什么吗?提前致谢!!

回答by Rui Peres

It would be like this:

它会是这样的:

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

Put it in your viewDidLoad, so you don't have problems with multiple events being fired. It can happen if you put it on viewWillAppearor viewDidAppear, and you are pushing or showing a modalViewController.

将它放在您的 中viewDidLoad,这样您就不会遇到触发多个事件的问题。如果您将其放在viewWillAppear或 上viewDidAppear,并且您正在推送或显示 modalViewController,则可能会发生这种情况。

回答by MadhavanRP

Hymany Boy's answer will get your job done. An alternate solution(if you're keen on using performSelectormethod) would be to add the same line in your method definition like so

Hymany Boy 的回答将完成您的工作。另一种解决方案(如果您热衷于使用performSelector方法)是在您的方法定义中添加相同的行,如下所示

-(void) getMatchListWS {
//Get Match List here

[self performSelector:@selector(getMatchListWS) withObject:nil afterDelay:1.0];
}

Note: You should still call the method once when the view loads.

注意:当视图加载时,您仍然应该调用一次该方法。

回答by Anil

You are just keeping a delay in your call. What that will do, is to call your method after a delay of 1 second. What you need to do is to set the timer to call your method repeatedly after a particular time interval.

您只是在通话中保持延迟。这样做是在延迟 1 秒后调用您的方法。您需要做的是将计时器设置为在特定时间间隔后重复调用您的方法。

//create an instance of NSTimer class
NSTimer *timer;

//set the timer to perform selector (getMatchListWS:) repeatedly
timer= [NSTimer timerWithTimeInterval:1.0
                                        target:self
                                        selector:@selector(getMatchListWS:)
                                        userInfo:nil
                                        repeats:YES];