【问题标题】:iPhone - Help with MBProgressHUD, Sudzc, Web Services and NSThreadiPhone - MBProgressHUD、Sudzc、Web 服务和 NSThread 的帮助
【发布时间】:2011-02-01 09:43:49
【问题描述】:

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

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

http://www.sudzc.com/

..以及活动指示器的以下MBProgressHUD

https://github.com/jdg/MBProgressHUD

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

有人可以指导我如何修复以下代码以使其正常工作吗?我认为这可能与显示进度指示器后触发回调方法有关。不太清楚如何使用回调方法正确实现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
}

【问题讨论】:

    标签: iphone objective-c web-services nsthread uiactivityindicatorview


    【解决方案1】:

    如果您在执行回调的情况下使用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 何时显示和关闭。至少,它会帮助您通过查看使用新方法后发生的变化来调试真正的问题所在。

    【讨论】:

      【解决方案2】:

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

      所以不要使用performSelectorOnMainThread。而是直接调用方法connectToService。当您调用showWhileExecuting 时,MBProgressHUD 将自行分离一个新线程。

      【讨论】:

      • 您正在谈论执行以下操作: [HUD showWhileExecuting:@selector(connectToService) onTarget:self withObject:nil animated:YES] 这不像我期望的那样工作,因为HUD 仅出现片刻,然后在显示结果之前消失,并且永远不会触发回调方法。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-01
        • 1970-01-01
        • 2011-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-21
        相关资源
        最近更新 更多