2011-01-29 80 views
0

这里是我的情况:我正在做同步HTTP请求来收集数据,但在手之前,我想要在导航栏标题视图中放置加载视图。请求结束后,我想将titleView返回到nil。iPhone navigationBar 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]完成。

感谢您的时间。

回答

1

你不能在同步方法中做到这一点。 当你需要发送[自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]; 
}