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:。もっときれいなものが必要な場合は、@evnaz が投稿した github リンクをチェックして、UIActivityIndicatorViewこの同じフローの代わりにそれを使用してください

于 2014-12-22T17:16:40.210 に答える