有循环代码时活动指示器不显示? [英] Activity indicator not showing when there is loop code?

查看:50
本文介绍了有循环代码时活动指示器不显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在欢迎屏幕和主屏幕中显示活动"指示器.在欢迎屏幕上工作正常.在主视图中,有一个"for"循环代码,可将某项任务执行16次.在这个时候,设备看起来有点挂起,以避免我想要显示活动指示器.当我在进行某些循环(16次)的代码期间在主屏幕中添加标准活动代码时,我没有观察到设置的活动指示器显示.它仅在循环完成之后才出现.我尝试在执行循环代码之前给一些延迟,但是在预期时间内仍然没有显示.否则,用户可能会认为我的应用程序挂起了设备,因此我想在那时显示活动指示器.

I am showing an Activity indicator in welcome screen as well as main screen. It is working fine in welcome screen. In main view, there is a 'for' loop code which does some task for 16 times. At this time, device looks like a bit hang, to avoid that i want to show activity indicator. When i add the standard activity code in main screen during some loop(16 times) code going on, i'm not observing the set activity indicator show. It comes only after the loop finished. I tried giving some delay before doing loop code, but still doesn't show during expected time. Otherwise user may think that my app hangs the device, so i want to show activity indicator at that time.

欢迎提出任何解决此问题的建议!

Any suggestions to resolve this are welcome !

谢谢.

推荐答案

发生的事情是,当您的程序阻塞主运行循环时,UIKit直到代码完成后才在指示器上调用start.解决此问题的方法是,使用以下命令使循环在后台线程中运行:

What happens is, when your program blocks the main run loop, UIKit does not call start on the indicator until your code is done. The way to get around this is, make your loop run in a background thread by using the:

-(void)performSelectorInBackground:(SEL)带有对象的Selector:(id)arg

关于NSObject的方法,这应该解决您的问题.

method on NSObject, this should take care of your problem.

在此处链接到该方法的文档:

Heres a link to the docs on the method:

查看全文

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