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すべての Web サイトのコンテンツをローカル ディレクトリに保存しています。保存が完了したら、progressHUD が消えたらいいのにと思います。

ありがとう

4

3 に答える 3

5

割り当てに時間がかかる場合があり、割り当てるまでにタスク全体が終了することがあるためHUDviewWillAppear:代わりに の割り当てを試してください。-(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 が表示されます。メイン スレッドは、savingPage メソッドによって実行されます。

これでもうまくいかない場合は、に変更waitUntilDone:YESしてくださいwaitUntilDone:NO

注: Apple のドキュメントによると

重要 自動参照カウント (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 を使用しない場合でも使用できます。

お役に立てれば。

于 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 に答える