iPhone导航栏标题视图同步请求问题

发布于 2024-10-14 22:04:06 字数 497 浏览 2 评论 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] 完成。

谢谢你的时间。

Here's my situation: I am making synchronous HTTP requests to collect data but before hand I want to place a loading view within the navigation bar title view. After the request is over I want to return the titleView back to 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.

I know the loading view works because after the request is over the loading view is shown.

My Problem: It should be obvious at this point but basically I want to delay the
[self makeHTTPconnection] function until the [self showLoading] has completed.

Thanks for you time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

壹場煙雨 2024-10-21 22:04:06

您无法通过同步方法来做到这一点。
当您发送[self showLoading]消息时,在整个方法完成之前UI不会更新,因此它已经完成了其他两个任务(makeHTTPConnection和< em>结束加载)。因此,您永远不会看到加载视图。

这种情况的一个可能的解决方案是并发工作:

[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];
}

You can't do that in a synchronous approach.
When you would send [self showLoading] message, the UI wouldn't be updated until the entire method finishes, so it would already finish the other two tasks (makeHTTPConnection and endLoading). As a result, you would never see the loading view.

A possible solution for this situation would be working concurrently:

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

Then you must to add the *_sendRequest* method:

- (void)_sendRequest
{
    [self makeHTTPConnection];
    //[self endLoading];
    [self performSelectorOnMainThread:@selector(endLoading) withObject:nil waitUntilDone:YES];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文