gpt4 book ai didi

ios - 有什么方法可以添加事件指示器以等待从 nsurlconnection 中获取完整的数据

转载 作者:行者123 更新时间:2023-11-29 02:25:28 25 4
gpt4 key购买 nike

我有一个登录页面,当我按下“登录”按钮时,我必须调用网络服务并根据响应加载新 View 我想显示加载 View 的事件指示器需要 3 到 4 秒

如何证明

我正在使用带有 NSURLConnection 委托(delegate)的异步 NSURLConnection

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[conn start];

didFinishLoading

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSError *e = nil;
res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&e];

//[indicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];

NSLog(@"data is %@",res);
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"create.json"];

NSLog(@"file path is %@ ",path);

NSFileManager *fileManager=[NSFileManager defaultManager];

if(![fileManager fileExistsAtPath:path])
{

NSString *bundle = [[[NSBundle mainBundle]resourcePath]stringByAppendingString:@"create.json"];

[fileManager copyItemAtPath:bundle toPath:path error:&error];
}


[res writeToFile:path atomically:YES];

如何在此 View 中显示

我卡在这里

最佳答案

在你的@interface中,添加:

@property (nonatomic, strong) UIActivityIndicatorView *ai;

当您开始连接时,启动您的微调器:

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[conn start];
_ai = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_ai.frame = CGRectMake(0, 0, 24, 24); // adjust the frame to where you want the spinner to appear
[_ai startAnimating];
[self.view addSubview:_ai];

当你的连接完成后,将其移除:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[_ai stopAnimating];
[_ai removeFromSuperView];

... // all your other code
}

确保在 connectionDidFailWithError: 回调中以相同的方式删除它。如果你想要更漂亮的东西,请查看@evnaz 发布的 github 链接并使用它代替 UIActivityIndi​​catorView 和相同的流程

关于ios - 有什么方法可以添加事件指示器以等待从 nsurlconnection 中获取完整的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27598787/

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