这是我的情况:我正在制作同步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]
,直到{{1}}完成。
谢谢你的时间。
答案 0 :(得分:1)
你不能用同步方法做到这一点。 如果要发送 [self showLoading] 消息,则在整个方法完成之前不会更新UI,因此它已经完成了其他两个任务( makeHTTPConnection 和< EM> endLoading )。因此,您永远不会看到加载视图。
这种情况的可能解决方案是同时工作:
[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];
}