gpt4 book ai didi

ios - 为什么加载数据指示器无法正常工作?

转载 作者:行者123 更新时间:2023-12-01 19:12:48 25 4
gpt4 key购买 nike

在我的代码中,我在UIAlertView上使用UIActivityIndi​​catorView。它工作正常,但我的问题是未在正确的时间显示。我的意思是说,当设备从Web服务获取数据之后,此加载指示符最后出现,并且这不是礼貌的事情,因为我希望它在Web服务发送或接收数据时出现。

我是iOS应用开发的新手,需要帮助。如果还有其他简单的方法可以做到这一点,那么建议我。
我希望我的问题很清楚,我的问题是根据此代码,当我从Web服务获得答复后,正在显示加载指示符,但我想运行此指示符,因为用户将按“更新”按钮,然后应调用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;

}

最佳答案

您可以使用GCDRaywenderlich Tutorial

-(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
*/
});
});
}

关于ios - 为什么加载数据指示器无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15310797/

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