Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion failure using MBProgressHUD - View must not be nil

I am trying to use the MBProgressHUD within an application. I am getting an error at the point the HUD is added to the view.

This is the code that adds the progress bar to the view.

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

// Set determinate mode
HUD.mode = MBProgressHUDModeAnnularDeterminate;

HUD.labelText = @"Loading";

// myProgressTask uses the HUD instance to update progress
[HUD showWhileExecuting:@selector(processFieldEntries) onTarget:self withObject:nil animated:YES];

The application errors with:

*** Assertion failure in -[MBProgressHUD initWithView:], /Users/.../MBProgressHUD/MBProgressHUD.m:190

Also

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'View must not be nil.'

And

Crash: View must not be nil.

Does anyone know what the Assertion failure is for and how to resolve. The MBProgressHUD.m file is included in the Compile Sources under the Build Phases tab and header included in the file. The progress is being added to a process for processing field validation.

like image 546
StuartM Avatar asked Aug 27 '12 21:08

StuartM


2 Answers

Hi here is a quick tip on the HUD display.

first, not trying to be flip here, but make sure that if you are initializing the HUD for a navigation controller, that you have one - or anything else for that matter. Note, the higher you stick the HUD in your view, the more interaction that will get disabled and covered by the HUD overlay (which is a good thing usually).

so for example if your in a basic view controller, or a modal, etc. do something like this:

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

note that you are adding it to the same view you initialized it with.

also note you can stuff it in other views as well like: self.navigationcontroller.view, self.splitviewcontroller.view or my favorite: self.splitviewcontroller.view.superview (to cover and disable both sides of the view).

I think your issue will resolve itself if you follow init example with the correct view for your app.

be well.

like image 77
CocoaEv Avatar answered Oct 24 '22 15:10

CocoaEv


Where do you use that code snippet? If you provide some other details I could help you. Meanwhile, waiting for some details, I can give you some hints on your problem.

I guess that when you create the MBProgressHUD

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

the view for your navigation controller is nil and hence the problem.

If you navigate the MBProgressHUD code (MBProgressHUD.m) you can see that there is a control check like this:

NSAssert(view, @"View must not be nil.");

and so the code stops since it does not pass the control.

To make this working you need to pass a non-nil view.

If you have created a class that extends a UIViewController (for example) you could present the HUD in viewDidAppear method. There you are sure that the view for your view controller has been created and displayed.

Alternatively, add the HUD as a subview of the window. For further info you could see MBProgressHUD not showing.

Hope that helps.

like image 1
Lorenzo B Avatar answered Oct 24 '22 15:10

Lorenzo B