我有一个方法,可以从 Web 服务加载数据。 在该方法的开始,我喜欢显示一个 UIActivityIndicator。
但仅将隐藏更改为NO并不会重绘UIView。 因此 UIActivityIndicator 在长时间加载数据后才可见,这肯定已经太晚了。
那么我怎样才能告诉 UIView 在数据加载开始之前重绘 ActivityIndicator 呢?
- (IBAction)loadData{
[activity setHidden:NO];
// here the activtyIndicator should appear
...
loadlongdataFromNet..
[activity setHidden:YES];
// here the activtyIndicator should disappear
}
最佳答案
切勿在主线程上执行网络和其他计算密集型操作!!!
它将卡住/阻止用户界面。您应该将与网络相关的长处理移至后台线程:
- (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设置隐藏:NO at begin of a long method does not show on display,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13124226/