2

I'm using SDWebImagePrefetcher to prefetch a list of images off my plist.

I was editing the method trying to alloc a UIViewController which would have shown an MBProgressHUD HUD to indicate the progress.

Problem is that the HUD won't show in any way. Plus, SDWebImagePrefetcher is a NSObject and I don't really know where to init my custom UIViewController and the HUD, due it doesn't have any viewDidLoad or viewWillAppear methods.

I tried to do something like

MyCustomViewController *cv = [[MyCustomViewController alloc]init];
HUD = [[MBProgressHUD alloc]initInView:cv.view];

and didn't work.

I also tried to add an UIAlertView as subView of the viewController in order to see if it worked but it didn't work at all!

Any advice on where to init an UIViewController in a NSObject class in order to show an HUD on the allocated controller would be awesome.

Phillip
  • 4,276
  • 7
  • 42
  • 74
  • did you make sure your `cv` is not nil?? also, after you create that view controller, how do you present it? – jere Sep 29 '12 at 19:52
  • It's not nil, and I don't need to present it due this class is a singleton which i need to do what's descripted here http://stackoverflow.com/questions/12439536/mbprogresshud-and-sdwebimageprefetcher (Stig Brautaset last comment) – Phillip Sep 29 '12 at 20:01
  • stig's comment has nothing to do with what you are asking here... he's talking about how you should update the progress for your HUD. you say that you can't see what you add to your view controller... what is it that you exactly want to do? – jere Sep 29 '12 at 21:36
  • And indeed. I want to update its progress,the HUD doesn't even show up, but i kinda got the mechanism of it should work. I just want to show an HUD in class A where class A is allocated in class B (singleton) – Phillip Sep 29 '12 at 23:49

1 Answers1

4

If you want to display a MBProgressHUD from any class it's simple. Just add it to your application's keyWindow. This is the very top level window for your application and it easy to access from anywhere.

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:keyWindow animated:YES];
hud.labelText = @"Loading";

And when you want to hide the MBProgressHUD just use

[hud hide:YES];

Make sure you wait to access the keyWindow until your app's main window has been set up. This occurs at launch. If it is setup programmatically it is in your AppDelegate by calling [window makeKeyAndVisible].

brynbodayle
  • 6,546
  • 2
  • 33
  • 49