gpt4 book ai didi

iphone - ios setHidden :NO at begin of a long method does not show on display

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:48:31 25 4
gpt4 key购买 nike

我有一个方法,它从 Web 服务加载数据。在该方法的开始,我想显示一个 UIActivityIndi​​cator。

但是只把hidden改成NO并不会重绘UIView。所以 UIActivityIndi​​cator 在长时间加载数据后可见,这肯定是迟到了。

那么我如何告诉 UIView 在数据加载开始之前重绘 activityIndi​​cator?

- (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com