0

这里说使用 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];
}

但仍然没有运气。有人让我知道我在这里缺少什么吗?

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

谢谢

4

3 回答 3

5

Try allocation of HUD on the viewWillAppear: instead of -(IBAction)save because sometimes allocation takes up the whole time and by the time it allocates whole task is finished.

Copy Following links to viewWillAppear: and remove them from -(IBAction)save

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

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

EDIT: Keep allocations on viewWillAppear: and change code as shown below:

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

What this will do is create a separate thread to display HUD as main thread is being engaged by savingPage method.

If this too doesn't work, then just change waitUntilDone:YES to waitUntilDone:NO

Note: As per Apple documentation

Important If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use @autoreleasepool blocks instead. For example, in place of:

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

you would write:

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

@autoreleasepool blocks are more efficient than using an instance of NSAutoreleasePool directly; you can also use them even if you do not use ARC.

Hope this helps.

于 2012-04-01T18:49:42.587 回答
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];
    }
于 2012-04-01T18:18:16.263 回答
1

尝试将 HUD 分配线更改为

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

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

[MBProgressHUD hideHUDForView:self.navigationcontroller.view animated:NO];
于 2012-04-01T17:38:54.630 回答