在我的代码中,我在 UIAlertView 上使用了 UIActivityIndicatorView。它工作正常,但我的问题是它没有在正确的时间出现。我的意思是说,当设备从 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;
}