gpt4 book ai didi

ios - 获取 JSON 数据时显示事件指示器

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:50:37 24 4
gpt4 key购买 nike

我是 IOS 编程的初学者。我的问题是我的应用程序从我的网络服务器中的 JSON 获取数据,当启动应用程序时,由于获取过程,它会稍微滞后和延迟,所以我想在连接到 JSON 数据时显示事件指示器。我该怎么做?

我的 JSON 编码:

- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *urlAddress = [NSURL URLWithString:@"http://emercallsys.webege.com/RedBoxApp/getEvents.php"];
NSStringEncoding *encoding = NULL;
NSError *error;

NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:urlAddress usedEncoding:encoding error:&error];
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];

NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
if (dict)
{
eventsDetail = [[dict objectForKey:@"eventsDetail"] retain];
}

[jsonreturn release];
}

最佳答案

使用下面的代码

//add a UIActivityIndicatorView to your nib file and add an outlet to it
[indicator startAnimating];
indicator.hidesWhenStopped = YES;

dispatch_queue_t queue = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
//Load the json on another thread
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:urlAddress usedEncoding:encoding error:NULL];
[jsonreturn release];
//When json is loaded stop the indicator
[indicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
});

关于ios - 获取 JSON 数据时显示事件指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11013442/

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