0

这是我的情况:我正在发出同步 HTTP 请求来收集数据,但在此之前我想在导航栏标题视图中放置一个加载视图。请求结束后,我想将 titleView 返回为零。

[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,因此它已经完成了其他两个任务(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 回答