0

我有一个登录页面,当我按下“登录”按钮时,我必须调用 Web 服务并根据响应加载新视图,这需要 3 到 4 秒我想显示加载视图的活动指示器

如何证明

我正在使用带有 NSURLConnection 委托的异步 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];

如何在此视图中显示

我卡在这里

4

1 回答 1

1

在您的@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:确保在回调中也以相同的方式删除它。如果您想要更漂亮的东西,请查看发布的 github 链接 @evnaz 并使用它而不是UIActivityIndicatorView使用相同的流程

于 2014-12-22T17:16:40.210 回答