0

タブバーでナビゲートされる教育アプリで MBProgressHUD を使用しています。

ユーザーは、Urban Airship のストアフロントを介してテーブルビューから UA の詳細ビューに直接移動します。購入をクリックすると、HUD が表示されます

 HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
 [self.view.window addSubview:HUD];

showWhileExecutingステートメントを使用しています。

"Connecting" から "Downloading" から "Unpacking" に変わるには、3 つの while ステートメントが必要です。すべて正常に動作しています。

ここで問題が発生します...これを2回目にすると、ラベルのテキストは変更されません。「接続中」で止まっています。NSLog で、他のループを通過していることがわかります。
その上、モードを変更しようとすると、アプリがクラッシュします。

これは 2 回目以降の使用時にのみ発生します。アプリを強制終了すると、すべてが初めて機能します。

MBProgressHUD が終了してもリセットされないように見えます。
(ARCはプロジェクトで使用されています)

解決策を持っている人はいますか?ありがとう

編集:

- (void)showWithLabelDeterminate 
{

    HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
    HUD.mode = MBProgressHUDModeIndeterminate;
    [self.view.window addSubview:HUD];


    HUD.delegate = self;
    HUD.labelText = NSLocalizedString(@"Connecting","");
    HUD.detailsLabelText = @" ";
    HUD.minSize = CGSizeMake(145.f, 145.f);
    HUD.dimBackground = YES;

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


-(void)lessonDownloadProgress
{

    DataManager *sharedManager = [DataManager sharedManager];
    //  HUD.mode = MBProgressHUDModeIndeterminate;
    HUD.labelText = nil;
    HUD.detailsLabelText = nil;

    while ([sharedManager.downHUD floatValue] == 0.0f) 
    { 
        [self parentViewController];
        NSLog(@"HUD lessonDownloadProgress: %f", HUD.progress);
        HUD.labelText = NSLocalizedString(@"Connecting","");
        HUD.detailsLabelText = @" ";
        NSLog(@"Waiting for download to start");
        //  Wait for download to start
        usleep(80000);
    }

    // Switch to determinate mode     
    // HUD.mode = MBProgressHUDModeDeterminate;
    HUD.labelText = NSLocalizedString(@"DownLoading","");
    HUD.progress = [sharedManager.downHUD floatValue];

    while (HUD.progress < 1.0f && [sharedManager.cleanedUp isEqualToString:@"No"])
    {
        //  [self parentViewController];
        HUD.labelText = NSLocalizedString(@"Downloading","");
        NSLog(@"HUD lessonDownloadProgress: %f", HUD.progress);
        HUD.progress = [sharedManager.downHUD floatValue];                       
        NSString *percent = [NSString stringWithFormat:@"%.0f", HUD.progress/1*100];
        HUD.detailsLabelText = [percent stringByAppendingString:@"%"];
        usleep(50000);
    }

    //  Switch HUD while cleanUp
    HUD.mode = MBProgressHUDModeIndeterminate;

    while ([sharedManager.cleanedUp isEqualToString:@"No"]) 
    {
        [self parentViewController];
        HUD.labelText = NSLocalizedString(@"Unpacking","");
        HUD.detailsLabelText = @" ";
        //  wait for cleanup
        NSLog(@"Waiting for clean up");
        usleep(50000);
    }

    NSLog(@"++ Finished loops ++");
    NSLog(@"Finished HUD lessonDownloadProgress: %f", HUD.progress);

    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [HUD removeFromSuperview];
    HUD.delegate = nil;

    [HUD release];
    HUD = nil;

}
4

3 に答える 3

0

デリゲートメソッドを実装しましたか?

- (void)hudWasHidden:(MBProgressHUD *)hud {
    // Remove HUD from screen when the HUD was hidded
    [HUD removeFromSuperview];
    HUD = nil;
}
于 2012-03-15T14:42:54.853 に答える
0

これは、ラベルが init で設定されているためです。これを試してください:

このメソッドを MBProgressHud のヘッダー ファイルに追加するだけです。

+ (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view withText:(NSString *)text;

そして、次のように .m ファイルに実装します。

+ (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view withText:(NSString *)text
{
    MBProgressHUD *hud = [[self alloc] initWithView:view];
    hud.labelText = text;
    [view addSubview:hud];
    [hud show:YES];
    return MB_AUTORELEASE(hud);
}

好きな場所で呼び出します:

[MBProgressHUD showHUDAddedTo:self.view withText:@"Loading..."];
于 2014-02-16T02:17:45.503 に答える
0

あなたが投稿したコードで問題を見つけることができません。ただし、いくつかのリファクタリングが役立つ場合があります。

をポーリングする代わりに、 DataManagerKVO を使用して のプロパティを観察し、DataManagerそれらの変更に対応することができます。 (「電話しないでください。電話します。) したがって、必要に応じて提案されるアプローチを次に示します.

あなたのクラスインターフェース:

@interface YourClass : UIViewController    // or whatever your superclass is...
{
    MBProgressHUD *_hud;
    DataManager *_dataManager;

    //  your other ivars
}
@end

そして、実装ファイルで...

@interface YourClass()
@property (nonatomic, retain) DataManager dataManager;
@end

上記で、dataManager をプロパティとして宣言したので、それを観察できます。

ダウンロード プロセスを開始するためのメソッドが用意されましたdownloadLesson

- (void)downloadLesson;
{
    //  show HUD and retain it (showHUDAddedTo:animated: returns autoreleased object)
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];

    //  observe properties on the dataManager
    [self addObserver:self forKeyPath:@"dataManager.progress" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"dataManager.cleanedUp" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"dataManager.downHUD" options:NSKeyValueObservingOptionNew context:nil];

    //  begin your download here...

    HUD.labelText = NSLocalizedString(@"Connecting", "");
    HUD.detailsLabelText = @" ";
    HUD.progress = self.dataManager.downHUD;
}

KVO を使用して、HUD の外観を更新します。

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
    if( [keyPath isEqualToString:@"dataManager.cleanedUp"] )
    {
        if( [[[self dataManager] cleanedUp] isEqualToString:@"Yes"] )
        {
            [MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] keyWindow] animated:YES];
            [HUD release];  HUD = nil;
            [self removeObserver:self forKeyPath:@"dataManager.progress"];
            [self removeObserver:self forKeyPath:@"dataManager.cleanedUp"];
            [self removeObserver:self forKeyPath:@"dataManager.downHUD"];
        }
    }
    if( [keyPath isEqualToString:@"dataManager.downHUD"] )
    {
        //  if the data manager updates progress, update our HUD
        HUD.progress = self.dataManager.downHUD;
        if( self.dataManager.downHUD == 0.0 )
            //  no progress; we're just connecting
            HUD.labelText = NSLocalizedString(@"Connecting", "");
        else if( self.dataManager.downHUD < 1.0 )
        {
            //  progress >0.0 and < 1.0; we're downloading
            HUD.labelText = NSLocalizedString(@"Downloading", "");
            NSString *percent = [NSString stringWithFormat:@"%.0f%%", HUD.progress/1*100];
            HUD.detailsLabelText = percent;
        }
        else
        {
            //  progress == 1.0, but we haven't cleaned up, so unpacking
            if( [[[self dataManager] cleanedUp] isEqualToString:@"No"] )
            {
                HUD.labelText = NSLocalizedString(@"Unpacking","");
                HUD.detailLabelsText = @" ";
            }
        }
    }
}

または、通知を使用して更新を行うこともできます。この場合、View Controller が登録されているDataManager投稿はs です。NSNotificationまたは、リファクタリングを受け入れるDataManager場合は、ブロックを使用して更新を行うことができます。これらのソリューションはすべて、スレッドを明示的にブロックしてDataManager. お役に立てれば。

于 2012-03-15T12:55:55.200 に答える