alfreedom
alfreedom

Reputation: 441

Reusable MBProgressHUD

I want to make a "reusable" MBProgressHUD. For example, I have a code mostly like this.

- (void)viewDidLoad {
    HUD = [[MBProgressHUD alloc] init];
    HUD.delegate = self;

    [self functionOne];
}

- (void)functionOne {
    //turn on HUD
    HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    HUD.labelText = @"Loading with message 1";

    NSURLSessionDataTask *dataTask = [defaultSession 
        dataTaskWithRequest:urlRequest 
        completionHandler:^(NSData *dataRaw, NSURLResponse *header, NSError *error) 
            {
                //on completion turn off HUD and call functionTwo
                [HUD show:NO];
                [self functionTwo];
            }
    ];
}

- (void)functionTwo {
    //turn on again THE SAME HUD
    HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    HUD.labelText = @"Loading with message 2";

    NSURLSessionDataTask *dataTask = [defaultSession 
        dataTaskWithRequest:urlRequest 
        completionHandler:^(NSData *dataRaw, NSURLResponse *header, NSError *error) 
            {
                //on completion turn off THE SAME HUD
                [HUD show:NO];
            }
    ];
}

The problem is the code crash on //turn on again THE SAME HUD with error:

Assertion failure in -[MBProgressHUD initWithView:] - Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'View must not be nil.'

I dont know why, specially if I alloc and init it in viewDidLoad.

Another things is I don't understand the difference between [HUD hide:YES] and [HUD show:NO]

EDIT: this is my original code.

@implementation ReservasViewController
{
NSMutableArray *arrayPartidos;
MBProgressHUD *HUD;
NSInteger idPartidoEstado;
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:NO animated:NO];
self.navigationController.navigationBarHidden = NO;
self.navigationController.navigationBar.hidden = NO;

[self obtenerPartidosJugador];
}

-(void)obtenerPartidosJugador
{
HUD = [[MBProgressHUD alloc] init];
HUD.delegate = self;
HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
HUD.labelText = @"Cargando partidos...";

//----------------------
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%s%s", root_server, obtener_reservas]];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];

NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *dataRaw, NSURLResponse *header, NSError *error) {

    NSDictionary *respServidor = [NSJSONSerialization JSONObjectWithData:dataRaw options:0 error:&error];
    NSLog(@"respServidor %@", respServidor);

    if(!error){

        if([[respServidor valueForKey:@"status"]  isEqual: @"true"]){

            arrayPartidos = [[NSMutableArray alloc] init];
            arrayPartidos = [[respServidor objectForKey:@"partidos"] mutableCopy];

            [self.tableView reloadData];

        }else{
            arrayPartidos = nil;
        }

    } else {
        NSLog(@"error --> %@", error);
    }

    [HUD hide:YES];
    [HUD show:NO];

}];

[dataTask resume];
}

//this function is called by a IBAction
-(void)cambiarEstadoPartido:(NSInteger)estado idPartido:(NSInteger)idpartido
{
HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
HUD.labelText = @"Actualizando...";

NSString *noteDataString = [NSString stringWithFormat:@"estado=%ld&idpartido=%ld", estado, idpartido];
//----------------------
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%s%s", root_server, cambiar_estado_partido]];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[noteDataString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *dataRaw, NSURLResponse *header, NSError *error) {

    NSDictionary *respServ = [NSJSONSerialization JSONObjectWithData:dataRaw options:0 error:&error];
    NSLog(@"respServidor %@", respServ);

    if(!error){

        if([[respServ valueForKey:@"status"]  isEqual: @"true"]){

            [HUD hide:YES];
            [HUD show:NO];

            [self obtenerPartidosJugador];

        }else{

            [HUD hide:YES];
            [HUD show:NO];

            UIAlertView *alertConfirm = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Ocurrio un error, vuelve a intentarlo" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            alertConfirm.tag = 0;
            [alertConfirm show];

        }

    } else {

        [HUD hide:YES];
        [HUD show:NO];

        NSLog(@"error --> %@", error);
        UIAlertView *alertConfirm = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Ocurrio un error, vuelve a intentarlo" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        alertConfirm.tag = 0;
        [alertConfirm show];

    }

}];

[dataTask resume];
}

EDIT 2: i remove all the lines that refer to the MBProgressHUD, i also remove the #import and the delegate but im still having that fu***ng error! enter image description here

Upvotes: 1

Views: 851

Answers (2)

alfreedom
alfreedom

Reputation: 441

OK i solved my issue. The problem was i have added an MBProgressHUD instance in navigationController.view in a previous UIVIewController.

 HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

So i think that previous "instance" of MBProgressHUD remained stuck to the navigationController.view an did break my application.

I hope i explained myself and I hope it will be useful to someone in the future!

Upvotes: 1

Hugues BR
Hugues BR

Reputation: 2258

[HUD hide:YES] hide the hud with an animation. [HUD show:NO] show the hug with no animation.

the naming of this API is a bit weird, it should have been something like: [HUD hideWithAnimated:], [HUD showWithAnimated:].

So you probably want to use [HUD hide:YES].. instead of [HUD show:NO]..

In functionTwo do you have a view? what happen if you NSLog(@"view: %@", self.navigationController.view)?

Also, why do you init you HUD in viewDidLoad if you reassign it after?

I think you may be confuse by the usage of MBProgressHUD.

Upvotes: 0

Related Questions