私のコードでは、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;
}