3

API からデータを読み込んでおり、UIProgressView を使用して読み込んだ量を表示しています。

私の viewWillAppear では、インターネット接続があることを確認するために Reachability を使用しています。次に、ある場合は、関数内で次の行が 10 回呼び出されます。

[self performSelectorInBackground:@selector(updateProgress) withObject:nil];

次に、このメソッドを実行します

-(void)updateProgress {
    float currentProgress = myProgressBar.progress;
    NSLog(@"%0.2f", currentProgress);
    [loadingProg setProgress:currentProgress+0.1 animated:YES];
}

float は 0.1 ずつ増加し、ローディング ビューにこれが表示されます。

ビューが閉じられ (モーダル ビューです)、呼び出されると、メソッドが実行され、NSLog は currentProgress が必要に応じて増加していることを示します。ただし、進行状況バーは空のままです。誰がこれを引き起こす可能性があるか知っていますか?

参考までに、私はARCを使用しています。

アップデート:

これが私がAPIを呼び出す方法です

NSString *urlString = **url**;
NSURL *JSONURL = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:JSONURL
                        cachePolicy:NSURLRequestReloadIgnoringCacheData 
                        timeoutInterval:10];
if(connectionInProgress) {
    [connectionInProgress cancel];
}
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

//This is where I call the update to the progress view

そして、私はこれらの機能を持っています:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    JSONData = [NSMutableData data];
    [JSONData setLength:0];
}

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [JSONData appendData:data];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    //add data to mutable array and other things
}
4

2 に答える 2

7

ユーザー インターフェイス (UI) コンポーネントを扱う場合、メイン スレッドでメソッドを実行する必要があります。原則として、プログラミングするときは、メイン スレッドで UI 操作を設定し、バックグラウンド スレッドで重くて複雑でパフォーマンスが要求される操作を設定する必要があります。 Central Dispatch. より長い操作を行う必要がある場合は、Ray Wenderlich のこの優れたチュートリアルを確認してください。)

これを解決するには、メソッドを呼び出し[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil];てから、次のメソッドを呼び出す必要があります。

-(void)updateProgress {
    float currentProgress = myProgressBar.progress;
    NSLog(@"%0.2f", currentProgress);
    dispatch_async(dispatch_get_main_queue(), ^{
    [loadingProg setProgress:currentProgress+0.1 animated:YES];
    });
}
于 2012-09-02T11:00:53.743 に答える
2

UI の更新はメイン スレッドで行う必要があります。変化する

[self performSelectorInBackground:@selector(updateProgress) withObject:nil];

[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];
于 2012-09-02T11:01:50.117 に答える