Search code examples
objective-cios6mbprogresshud

MBProgressHud with storyboards


The following code is from a sample demo.

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

HUD.delegate = self;
HUD.labelText = @"Loading";

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

It uses .xibs as it views. I'm creating an application that uses storyboards. Thus there is no navigationController for the initWithView method when instantiating HUD. Is there any way to implement a HUD without using .xibs and a navigationController. I've tried passing both "self" and "self.view" but they don't work. The class that this is in is the ViewController.m class and it is a UIViewController class. So I don't see why passing self wouldn't work.

This is my code

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

Again "self" is my main ViewController

Thanks!


Solution

  • Here's what I think you want:

    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    [view addSubview:hud];    
    [hud showWhileExecuting:@selector(YOUR_TASK) onTarget:YOUR_TARGET withObject:nil animated:YES];  // or NO if you don't want it to be animated
    

    Alternatively, if you want to manage showing and displaying the HUD manually yourself, there's some nice convenience methods for doing that:

    // To add HUD to a view
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];  // or NO if you don't want it to be animated
    
    // To set HUD text
    [hud setLabelText:@"Text"];
    
    // To remove HUD from a view (such as later in the code, after load, etc)
    [MBProgressHUD hideHUDForView:view animated:YES];
    

    Where view is the view you want the HUD added/removed from.

    I.e. self.view on a view controller.