3

我有以下代码,它连接到 Web 服务并返回一个类别数组。

我正在为 SOAP Web 服务使用以下包装器:

http://www.sudzc.com/

..以及MBProgressHUD活动指标的以下内容:

https://github.com/jdg/MBProgressHUD

我想要一个 HUD 进度指示器来指示它正在连接和获取结果。我目前正在使用MBProgressHUD以达到预期的效果,但是我注意到它不能正常工作。tableView在加载和显示my 中的实际单元格之前,进度指示器会消失。我也在MBProgressHUD我的应用程序的不同区域使用,但通常每次我连接到 Web 服务来获取结果。

有人可以指导我如何修复以下代码以使其正常工作吗?我认为这可能与显示进度指示器后触发回调方法有关。不太确定如何MBProgressHUD使用回调方法正确实现。

这是我“试图”让它发挥作用的可怕尝试。

- (void)showHUD {
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];

    // Add HUD to screen.
    [self.navigationController.view addSubview:HUD];

    // Register for HUD callbacks so we can remove it from the window at the right time.
    HUD.delegate = self;

    HUD.labelText = @"Loading";

    // Show the HUD while the provided method executes in a new thread
    [HUD showWhileExecuting:@selector(runLocalNotificationHandler) onTarget:self withObject:nil animated:YES];
}

- (void)runLocalNotificationHandler {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [self performSelectorOnMainThread:@selector(connectToService) withObject:nil waitUntilDone:YES];

    [pool release];
}

- (void)connectToService {
    Service1 *service = [[Service1 alloc] init];
    [service GetCategories:self action:@selector(handlerGetCategories:)];
    [service release];
}

- (void)handlerGetCategories:(id)value {
    // parse objects returned from array, populate array, reload table view data, etc
}
4

3 回答 3

6

如果您在MBProgressHUD执行回调的情况下使用,那么还有另一种方法。在开始后台进程之前初始化 HUD

MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

代替

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];

然后将以下内容放入您的回调方法中:

[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];

这样您就可以准确地知道您的 HUD 何时显示和关闭。至少,它将通过查看使用新方法后发生的变化来帮助您调试真正的问题所在。

于 2011-02-07T02:37:02.673 回答
1

不要在主线程上做网络的东西,因为这会阻塞 UI。在您的情况下,MBProgressHUD将不会设置动画,甚至不会显示。

所以不要使用performSelectorOnMainThread. 而是直接调用该方法connectToService。当MBProgressHUD您调用showWhileExecuting.

于 2011-02-06T16:55:35.177 回答
0

最终通过修改我的 Utility 类来解决这个问题。我正在执行主选择器两次!

于 2011-02-07T08:53:24.777 回答