MBProgressHUD没有重置

时间:2012-03-14 10:18:21

标签: ios mbprogresshud

我在我们的教学应用程序中使用MBProgressHUD,这是标签栏导航。

用户将通过Urban Airship的店面从tableview直接进入UA详细视图。 点击购买后,我带上HUD

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

正在使用 showWhileExecuting 语句。

它通过三个while语句从“正在连接”更改为“正在下载”到“解包”。 一切正常。

问题出在这里...... 我第二次这样做标签文本不会改变。它停留在“连接”上。 我可以在NSLog中看到它正在经历其他循环 最重要的是,如果我尝试更改模式,应用程序崩溃。

这仅在第二次以及任何后续使用中发生。如果我杀了应用程序,第一次一切都会再次运行。

在我看来,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;

}

3 个答案:

答案 0 :(得分:0)

我无法在您发布的代码中发现问题;但是一些重构可能会有所帮助。

您可以使用KVO观察DataManager上的属性并响应这些更改,而不是轮询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 = @" ";
            }
        }
    }
}

或者,您可以使用通知进行更新,其中您的视图控制器已注册的DataManager个帖子NSNotification。或者,如果您愿意重构DataManager,则可以使用块来进行更新。所有这些解决方案都避免必须明确阻止您的线程轮询DataManager。希望这会有所帮助。

答案 1 :(得分:0)

您是否实施了委托方法?

- (void)hudWasHidden:(MBProgressHUD *)hud {
    // Remove HUD from screen when the HUD was hidded
    [HUD removeFromSuperview];
    HUD = nil;
}

答案 2 :(得分: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..."];