作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个方法,它从 Web 服务加载数据。在该方法的开始,我想显示一个 UIActivityIndicator。
但是只把hidden改成NO并不会重绘UIView。所以 UIActivityIndicator 在长时间加载数据后可见,这肯定是迟到了。
那么我如何告诉 UIView 在数据加载开始之前重绘 activityIndicator?
- (IBAction)loadData{
[activity setHidden:NO];
// here the activtyIndicator should appear
...
loadlongdataFromNet..
[activity setHidden:YES];
// here the activtyIndicator should disappear
最佳答案
切勿在主线程上执行网络和其他计算密集型操作!!!
它将卡住/阻止 UI。您应该将长时间的网络相关处理移至后台线程:
- (void)loadData
{
[activity startAnimating]; // use this instead of setHidden
[NSThread detachNewThreadSelector:@selector(reallyLoadData) toTarget:self withObject:nil];
}
- (void)reallyLoadData
{
// network-heavy processing
// then:
[activity performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
}
关于iphone - ios setHidden :NO at begin of a long method does not show on display,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13124226/
我是一名优秀的程序员,十分优秀!