2012-04-01 124 views
0

here中表示使用MBProgressHUD很容易。 但我做不到。无法显示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将所有网站内容保存到本地目录。我希望一旦保存完成,进度HUD消失。

感谢

+0

为什么使用'self.navigationController.view'来分配和初始化'HUD'?您需要使用viewController的视图对其进行初始化,您需要将其显示出来。 – 2012-04-01 17:31:04

+0

即使我用self.view的结果仍然相同 – dejoong 2012-04-01 18:33:23

回答

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]; 
} 

这是什么需要做的是建立一个当主线程正在使用savingPage方法时,单独的线程显示HUD。

如果这也不起作用,那么就改变waitUntilDone:YESwaitUntilDone: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,也可以使用它们。

希望这会有所帮助。

+0

@dejoong:参考我的答案。我编辑了它 – 2012-04-01 18:50:11

+0

@dejoong:这个答案对你有帮助吗? – 2012-04-01 18:54:57

+0

嗨,Parth,实际上我已经在ViewWillAppear中使用了HUD,以便在加载webview时显示进度。然后我有按钮来保存网页内容 - (IBAction)保存,在这里我想制作另一个进度条。所以,基于你的建议,我只需要从 - (IBAction)保存方法中删除这个区别(因为我已经在viewWillAppear中:)。这样,它仍然显示相同的结果,progressHUD在页面完全保存后显示,而不是在保存过程中。 (并且只显示少于1秒) – dejoong 2012-04-01 19:03:20

1

尝试HUD分配线更改为

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

然后在你的储蓄方法,你可以有

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

嗨tams,我把2显示进度的方法HUD,哪一个你回应?无论如何,我尝试了两个,但没有运气 – dejoong 2012-04-01 18:28:54

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]; 
    } 

当保存gpage方法将完成MBProgressHUD代表将被调用,所以在你的类实现这个委托方法,这会从您的视图中删除HUD

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

嗨Piyush,它崩溃时,我没有使用performFetchOnMainThread和HUD.navigationBar是不存在我的HUD类(0.5版)。无论如何,我仍然用你的方式仍然使用performFetchOnMainThread,仍然显示相同的结果。 – dejoong 2012-04-01 18:40:42

+0

问题是,您正在使用performSelectorOnMainThread方法调用主线程(不在后台线程)上的savingPage方法,由于此HUD不会显示,直到savingPage方法完成,并显示只有在savingPage方法完成后秒,所以尝试在后台线程而不是主线程上运行savingPage方法。 – 2012-04-01 19:00:40

+0

耶..达肯定是有道理的。但你知道我是如何防止这个错误 - >布尔_WebTryThreadLock(布尔),0x5b04fb0:试图从主线程或Web线程以外的线程获取网络锁。这可能是从辅助线程调用UIKit的结果。现在崩溃... 这发生了,当我没有使用performSelectorOnMainThread – dejoong 2012-04-01 19:17:47