0

私の状況は次のとおりです。データを収集するために同期 HTTP 要求を作成していますが、事前にナビゲーション バーのタイトル ビュー内に読み込みビューを配置したいと考えています。リクエストが終わったら、titleView を nil に戻したいです。

[self showLoading];        //Create loading view and place in the titleView of the nav bar.
[self makeHTTPconnection]; //Creates the synchronous request
[self endLoading];         //returns the nav bar titleView back to nil.

リクエストが終わった後にローディングビューが表示されるため、ローディングビューが機能することはわかっています。

私の問題: この時点で明らかなはずですが、基本的には、 関数が完了[self makeHTTPconnection]するまで関数を遅らせたいと考えて[self showLoading]います。

ありがとうございました。

4

1 に答える 1

1

同期アプローチではそれを行うことはできません。[self showLoading]メッセージを送信すると、UIはメソッド全体が終了するまで更新されないため、他の2つのタスク( makeHTTPConnectionendLoading )はすでに終了しています。その結果、読み込みビューは表示されません。

この状況で考えられる解決策は、同時に機能することです。

[self showLoading];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(_sendRequest) object:nil];
[queue addOperation:operation];
[operation release];

次に、* _sendRequest*メソッドを追加する必要があります。

- (void)_sendRequest
{
    [self makeHTTPConnection];
    //[self endLoading];
    [self performSelectorOnMainThread:@selector(endLoading) withObject:nil waitUntilDone:YES];
}
于 2011-05-05T15:07:36.233 に答える