尽管有线程,UIActivityIndicatorView 仍不显示
我已经尝试实施这个很长一段时间了,但没有得到令人满意的结果。
假设我有一个执行 HTTP 请求的方法(特别是 Twitter 更新),并假设我想在 HTTP 请求进行时显示 UIActivityIndicatorView (我知道它何时完成)因为在请求完成时会调用委托方法,无论结果是正面还是负面)。
我看到很多答案都说线程对于此类的实现是必要的。首先,我尝试在不同的线程中调用 startAnimating
方法,并直接调用 stopAnimating
方法(无需启动新线程)。之后我看到这个人是如何做到的,我我认为这更安全,因为我用两种不同的方法(twitter 更新的委托方法)启动和停止指标。
然而,这两种方法都没有给我想要的结果(活动指示器根本不显示)。我有什么遗漏的吗?
预先感谢您,如果我的问题太长,我深表歉意。
非常感谢您的帮助。
I've been trying to implement this for a long time and I have gotten no favorable results.
Say I have a method in the which an HTTP request is performed (specifically, a twitter update), and say I want to display a UIActivityIndicatorView
while the HTTP request is in progress (I know when it is done because there are delegate methods that are called when the request is done, either with positive results or negative ones).
I've seen many answers that say that threading is necessary for the implementation of this class. At first I tried calling the startAnimating
method in a different thread and the stopAnimating
method directly (without starting a new thread). After that I saw how this guy does it and I thought this was safer as I was starting and stopping the indicator in two different methods (the delegate methods for the twitter update).
However, none of this two ways of doing this have given me the results I want (the activity indicator does not show up at all). Is there anything I'm missing?
Thank you in advance and I apologize if my question is too long.
Your help is very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 UIKit 子类时绝对禁止使用线程。您可能已经看到有关 UIActivityIndicatorView 在内部使用线程的报告,但这绝不意味着您可以从多个线程访问该对象。所有 UIView 子类(包括 UIActivityIndicatorView)只能从主线程访问。这包括调用
-startAnimating
和-stopAnimating
。如果您重写代码,使得您只访问主线程上的活动视图,但它仍然无法工作,那么我猜测该视图要么没有添加到可见视图中,要么被另一个视图覆盖视图,或者有一个将自己置于其超级视图的可见区域之外的框架。
Threading is absolutely forbidden when working with UIKit subclasses. You may have seen reports that UIActivityIndicatorView uses threading internally, but in no way does that mean you can access the object from multiple threads. All UIView subclasses (including UIActivityIndicatorView) must only be accessed from the main thread. This includes calling
-startAnimating
and-stopAnimating
.If you rewrite your code such that you're only ever accessing the activity view on the main thread, and it still isn't working, then I would guess that the view was either not added to a visible view, is covered up by another view, or has a frame that puts itself outside of the visible area of its superview.
您不能在辅助线程中执行 UI 操作。
您应该在辅助线程中执行 HTTP 请求,同时从主线程调用活动视图。
我建议使用DSActivityView,它更容易使用。只需 1 行即可显示活动视图,1 行可隐藏。
You cannot perform UI stuff in a secondary thread.
You should perform your HTTP request in a secondary thread, while calling the activity view from the main thread.
I recommend using DSActivityView which is so much easier to use. Just 1 line to show an activity view, 1 line to hide.
MBProgressHUD 它也很容易,并且在辅助线程启动/完成时显示和隐藏自身。像这样的事情:
“Yourstuff”将在单独的线程上运行。
您不应在辅助线程中执行 UI 活动。
MBProgressHUD it's also easy and shows and hides itself when the secondary thread has started/finished. Something like this:
"Yourstuff" will run on a separate thread.
You should not perform UI activities in a secondary thread.