MBProgressHUD 崩溃“MBProgressHUD 初始化程序中使用的视图为零”

Posted

技术标签:

【中文标题】MBProgressHUD 崩溃“MBProgressHUD 初始化程序中使用的视图为零”【英文标题】:MBProgressHUD crash "The view used in the MBProgressHUD initializer is nil" 【发布时间】:2011-11-24 14:34:29 【问题描述】:

有人使用 MBProgressHUD 吗?我想在 MBProgressHUD librairie 提供的示例中使用动画 showWithLabelMixed。我将此代码从示例复制到我的项目中。

-(IBAction)showActivity:(id)sender

    NSLog(@"Show Activity");
    [self showWithLabelMixed];



- (void)showWithLabelMixed

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

    HUD.delegate = self;
    HUD.labelText = @"Conecting";
    HUD.minSize = CGSizeMake(135.f, 135.f);

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



- (void)myTask 
    // Do something usefull in here instead of sleeping ...
    sleep(3);


- (void)myProgressTask 
    // This just increases the progress indicator in a loop
    float progress = 0.0f;
    while (progress < 1.0f) 
        progress += 0.01f;
        HUD.progress = progress;
        usleep(50000);
    





- (void)myMixedTask 
    // Indeterminate mode
    sleep(2);
    // Switch to determinate mode
    HUD.mode = MBProgressHUDModeDeterminate;
    HUD.labelText = @"Progress";
    float progress = 0.0f;
    while (progress < 1.0f)
    
        progress += 0.01f;
        HUD.progress = progress;
        usleep(50000);
    
    // Back to indeterminate mode
    HUD.mode = MBProgressHUDModeIndeterminate;
    HUD.labelText = @"Cleaning up";
    sleep(2);
    // The sample image is based on the work by www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
    // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
    HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.labelText = @"Completed";
    sleep(2);

但是我遇到了崩溃

由于未捕获的异常而终止应用程序 'MBProgressHUDViewIsNillException', reason: '在 MBProgressHUD 初始化程序为零。 你能帮帮我吗?

【问题讨论】:

【参考方案1】:

你用来初始化 HUD 的视图是 nil 尝试初始化它

[[UIApplication sharedApplication] keyWindow]

【讨论】:

@DDanZimm 哦,是的,我没有导航栏控制器,我使用 tabBarController。我将初始化更改为 HUD = [[MBProgressHUD alloc] initWithView:self.tabBarController.view]; , 和 HUD = [[MBProgressHUD alloc] initWithView:self.view];在这两种情况下,我都没有错误,但什么也没有出现。你给我的初始化怎么用? 我必须使这个HUD = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:HUD]; 很高兴听到您发现了问题!如果你在美国,感恩节快乐!

以上是关于MBProgressHUD 崩溃“MBProgressHUD 初始化程序中使用的视图为零”的主要内容,如果未能解决你的问题,请参考以下文章

ios toast提示框(MBProgressHUD)

MBProgressHUD的基本使用

如何快速使用 MBProgressHUD

IOS 第三方框架-MBProgressHUD

MBProgressHUD ---

显示在 MBProgressHUD 顶部的 UITableView 标题