iPhone - 帮助MBProgressHUD,Sudzc,Web服务和NSThread

fuz*_*uzz 3 iphone web-services objective-c nsthread uiactivityindicatorview

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

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

http://www.sudzc.com/

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

https://github.com/jdg/MBProgressHUD

我想有一个HUD进度指示器表明它正在连接和抓取结果.我目前正在使用MBProgressHUD以达到预期的效果,但我注意到它不能正常工作.在tableView加载和显示我的实际单元格之前,进度指示器消失.我也在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
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 6

如果您在MBProgressHUD执行回调的情况下使用,则还有另一种方法.在开始后台处理之前初始化HUD

MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
Run Code Online (Sandbox Code Playgroud)

代替

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
Run Code Online (Sandbox Code Playgroud)

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

[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以确切地知道何时显示和关闭HUD.至少,通过查看使用新方法后的更改,它将帮助您调试真正问题的位置.