Ios 为什么加载数据指示器不能正常工作?
在我的代码中,我在UIAlertView上使用UIActivityIndicatorView。它工作正常,但我的问题是它没有在正确的时间出现。我的意思是说,当设备从web服务获取数据之后,这个加载指示器最终会出现,我认为这不是什么好事,因为我希望它在web服务发送或接收数据时出现 我需要帮助,因为我是iOS应用程序开发新手。如果有任何其他简单的方法来做这件事,然后建议我。 我希望我的问题是清楚的,我的问题是根据这段代码,加载指示器是在我得到web服务的回复后出现的,但我想运行这个指示器,因为用户将按下更新按钮,然后应该调用web服务。告诉我哪里错了 这是我正在使用的代码Ios 为什么加载数据指示器不能正常工作?,ios,objective-c,json,uialertview,uiactivityindicatorview,Ios,Objective C,Json,Uialertview,Uiactivityindicatorview,在我的代码中,我在UIAlertView上使用UIActivityIndicatorView。它工作正常,但我的问题是它没有在正确的时间出现。我的意思是说,当设备从web服务获取数据之后,这个加载指示器最终会出现,我认为这不是什么好事,因为我希望它在web服务发送或接收数据时出现 我需要帮助,因为我是iOS应用程序开发新手。如果有任何其他简单的方法来做这件事,然后建议我。 我希望我的问题是清楚的,我的问题是根据这段代码,加载指示器是在我得到web服务的回复后出现的,但我想运行这个指示器,因为用户
-(IBAction)update:(id)sender
{
av=[[UIAlertView alloc] initWithTitle:@"Updating Image..." message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
ActInd=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[ActInd startAnimating];
[ActInd setFrame:CGRectMake(125, 60, 37, 37)];
[av addSubview:ActInd];
[av show];
{
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
int gid=[defaults integerForKey:@"gid"];
NSString *gameid=[NSString stringWithFormat:@"%i", gid];
NSLog(@"%@",gameid);
img=mainImage.image;
NSData *imgdata=UIImagePNGRepresentation(img);
NSString *imgstring=[imgdata base64EncodedString];
NSLog(@"%@",imgstring);
NSString *escapedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)imgstring,
NULL,
CFSTR("!*'();:@&=+$,/?%#[]"),
kCFStringEncodingUTF8);
NSLog(@"escapedString: %@",escapedString);
@try
{
NSString *post =[[NSString alloc] initWithFormat:@"gid=%@&image=%@",gameid,escapedString];
NSLog(@"%@",post);
NSURL *url=[NSURL URLWithString:@"http://mywebspace/updategameimage.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %d", [response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300) {
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",jsonData);
NSInteger type = [(NSNumber *)[jsonData objectForKey:@"type"] integerValue];
NSLog(@"%d",type);
if (type==1) {
[self alertStatus:@"You can Keep on Drawing" :@"Sketch Updated"];
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Unable to connect with game." :@"Connection Failed!"];
}
}
[av dismissWithClickedButtonIndex:0 animated:YES];
[av release]; av=nil;
}
-(iAction)更新:(id)发件人
{
av=[[UIAlertView alloc]initWithTitle:@“更新图像…”消息:@“委托:自取消按钮:nil其他按钮:nil];
ActInd=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[ActInd startAnimating];
[ActInd setFrame:CGRectMake(125,60,37,37)];
[av addSubview:ActInd];
[视听节目];
{
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
int gid=[默认的integerworkey:@“gid”];
NSString*gameid=[NSString stringWithFormat:@“%i”,gid];
NSLog(@“%@”,配子ID);
img=mainImage.image;
NSData*imgdata=UIImagePngResentation(img);
NSString*imgstring=[imgdatabase64encodedstring];
NSLog(@“%@”,imgstring);
NSString*escapedString=(NSString*)CFURLCreateStringByAddingPercentEscapes(
无效的
(CFStringRef)imgstring,
无效的
CFSTR(“!*”():@&=+$,/?%#[]),
kCFStringEncodingUTF8);
NSLog(@“escapedString:%@”,escapedString);
@试一试
{
NSString*post=[[NSString alloc]initWithFormat:@“gid=%@&image=%@”,gameid,escapedString];
NSLog(@“%@”,post);
NSURL*url=[NSURL URLWithString:@”http://mywebspace/updategameimage.php"];
NSData*postData=[post数据使用编码:NSASCIIStringEncoding allowLossyConversion:是];
NSString*postLength=[NSString stringWithFormat:@“%d”,[postData长度]];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置url:url];
[请求设置HttpMethod:@“POST”];
[请求设置值:HttpHeaderField的postLength:@“内容长度”];
[请求设置值:@“应用程序/json”用于HttpHeaderField:@“接受”];
[请求设置值:@“应用程序/x-www-form-urlencoded”forHTTPHeaderField:@“内容类型”];
[请求setHTTPBody:postData];
NSError*error=[[NSError alloc]init];
NSHTTPURLResponse*响应=nil;
NSData*urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&响应错误:&错误];
NSLog(@“响应代码:%d”,[Response statusCode]);
如果([response statusCode]>=200&&[response statusCode]UI更新在主线程上完成。您已经在主线程上启动了活动指示器。这很好
现在,您正在主线程上进行同步网络调用。它应该是异步的。在收到来自网络调用的响应之前,您的主线程将保持忙碌,并且您的UI将不会更新
要更新UI,您可以使网络调用异步,也可以在单独的函数中启动活动指示器,然后通过PerformSelect:afterdelay方法延迟网络活动的调用。您可以使用
谢谢Dude@Sergy KuryanovThanky@Apurv你的解决方案也是正确的,但三行公认的答案正好解决了我的问题。谢谢你的帮助。
-(IBAction)update:(id)sender
{
/*
Setup indicator and show it
*/
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
/*
Do network call
*/
dispatch_async(dispatch_get_main_queue(), ^{
/*
Update UI
*/
});
});
}