MBProgressHUD没有显示

时间:2011-04-02 08:22:19

标签: iphone ios ipad ios4

在我的应用中,我正在加载资源密集视图,需要大约1到2秒才能加载。所以我将它加载到一个单独的线程中:

hud = [[MBProgressHUD alloc] init];
[hud showWhileExecuting:@selector(loadWorkbench:) onTarget:self withObject:nil animated:YES];

然而它永远不会出现,应用程序看起来冻结给最终用户。我出错了什么想法?

1 个答案:

答案 0 :(得分:18)

是。它不会出现,因为你永远不会告诉我将HUD添加为窗口的子视图。尝试类似的事情:

    // Should be initialized with the windows frame so the HUD disables all user input by covering the entire screen
    HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];

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

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

    HUD.labelText = NSLocalizedString(@"Loading Workbench", nil);
    HUD.detailsLabelText = NSLocalizedString(@"please wait", nil);

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

由于您将自己设置为HUD委托,因此还要添加以下委托方法:

- (void)hudWasHidden {
    // Remove HUD from screen 
    [HUD removeFromSuperview];

    // add here the code you may need

}

并记得在相应的头文件中添加MBProgressHUDDelegate