无法显示MBProgressHUD

时间:2012-04-01 17:26:02

标签: iphone objective-c mbprogresshud

here中说使用MBProgressHUD很容易。 但我无法做到。

这是我的代码:

- (IBAction)save{
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];
    HUD.delegate = self;
    [HUD show:YES];

    NSString *title = [page stringByEvaluatingJavaScriptFromString:@"document.title"];
    SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
    [savePage save];
    [HUD hide:YES];
}

savePage save方法运行期间未显示进度指示器,但在页面完全保存后显示(指示器显示不到1秒)。

我也尝试过这种方式:

- (IBAction)save {

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

   HUD.delegate = self;
   HUD.labelText = @"Saving...";

   [HUD showWhileExecuting:@selector(performFetchOnMainThread) onTarget:self withObject:nil animated:YES];
}

- (void) savingPage{
    NSString *title = [page stringByEvaluatingJavaScriptFromString:@"document.title"];
    SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
    [savePage save];
}

-(void) performFetchOnMainThread    {
    [self performSelectorOnMainThread:@selector(savingPage) withObject:nil waitUntilDone:YES];
}

但仍然没有运气。有谁让我知道我在这里缺少的地方?

P.S:savePage save将所有网站内容保存到本地目录。我希望一旦保存完成,progressHUD就会消失。

由于

3 个答案:

答案 0 :(得分:5)

尝试在HUD而不是viewWillAppear:上分配-(IBAction)save,因为有时分配占用了整个时间,并且当它分配整个任务完成时。

复制以下指向viewWillAppear:的链接,并将其从-(IBAction)save

中删除
 HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
 [self.navigationController.view addSubview:HUD];

 HUD.delegate = self;
 HUD.labelText = @"Saving...";

编辑:在viewWillAppear:上保留分配并更改代码,如下所示:

- (IBAction)save {
    [NSThread detachNewThreadSelector:@selector(showHUD) withObject:nil];
    [self performSelectorOnMainThread:@selector(savingPage) withObject:nil waitUntilDone:YES];
}

- (void) savingPage{
    NSString *title = [page stringByEvaluatingJavaScriptFromString:@"document.title"];
    SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
    [savePage save];
    [HUD hide:YES];
}

-(void)showHUD {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   [HUD show:YES];
   [pool release];
}

这样做会创建一个单独的线程来显示HUD,因为savePage方法正在使用主线程。

如果这也不起作用,那么只需将waitUntilDone:YES更改为waitUntilDone:NO

即可

注意:根据Apple documentation

  

重要提示如果使用自动参考计数(ARC),则不能   直接使用自动释放池。相反,您使用@autoreleasepool   而是阻止。例如,代替:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init;
// Code benefitting from a local autorelease pool.
[pool release];
     你会写:

@autoreleasepool {
    // Code benefitting from a local autorelease pool.
}
     

@autoreleasepool块比使用实例更有效   NSAutoreleasePool直接;即使你不这样做,你也可以使用它们   使用ARC。

希望这有帮助。

答案 1 :(得分:2)

查看此代码

- (IBAction)save{
        HUD = [[MBProgressHUD alloc] initWithView:self.view];
        HUD.graceTime     = .1;
        HUD.navigationBar = self.navigationController.navigationBar;
        HUD.labelText     = @"Saving";
        HUD.delegate      = self;
        [self.view addSubview:HUD];

         [HUD showWhileExecuting:@selector(savingPage) onTarget:self withObject:nil animated:YES];
    }


    - (void) savingPage{
        NSString *title = [page stringByEvaluatingJavaScriptFromString:@"document.title"];
        SavePageAs *savePage = [[SavePageAs alloc] initWithUrl:self.site directory:title];
        [savePage save];
    }

当saveingpage方法完成后,将调用MBProgressHUD委托,所以在你的类中实现这个委托方法,这将从你的视图中删除HUD

 -(void)hudWasHidden
    {
        [HUD removeFromSuperview];
    }

答案 2 :(得分:1)

尝试将HUD分配线更改为

HUD = [MBProgressHUD showHUDAddedTo: self.navigationcontroller.view animated:YES];
HUD.labelText....

然后在您的保存方法中,您可以

[MBProgressHUD hideHUDForView:self.navigationcontroller.view animated:NO];