2015-12-03 37 views
1

我想製作一個「可重用」的MBProgressHUD。例如,我的代碼大部分都是這樣的。可重複使用的MBProgressHUD

- (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]; 
      } 
    ]; 
} 

的問題是//turn on again THE SAME HUD代碼崩潰與錯誤:

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

我不知道爲什麼,特別是如果我的Alloc和viewDidLoad中初始化它。

另一個事情是我不明白[HUD hide:YES][HUD show:NO]

編輯之間的區別:這是我的原代碼。

@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]; 
} 

編輯2:我刪除所有參考MBProgressHUD行,我也去掉#進口和委託,但IM仍然具有復*** NG錯誤! enter image description here

+0

您是否試圖在導航視圖中添加進度?我認爲你的問題來自 – FabKremer

+1

是的,我在導航視圖中無處不在。第二個節目出現問題。你在哪裏添加它? – Quetool

+0

我使用'[MBProgressHUD hideHUDForView:theView animated:YES];'而不是show:方法。你可以試試嗎? – FabKremer

回答

0

[HUD hide:YES]用動畫隱藏hud。 [HUD show:NO]顯示沒有動畫的擁抱。

這個API的命名有點奇怪,它應該是這樣的:[HUD hideWithAnimated:],[HUD showWithAnimated:]

所以,你可能想使用[HUD hide:YES] ..而不是[HUD show:NO] ..

在functionTwo你有一個觀點?如果你NSLog(@"view: %@", self.navigationController.view)會發生什麼?

此外,如果您之後重新分配,您爲什麼會在viewDidLoad中啓動HUD?

我想你可能會被MBProgressHUD的使用所迷惑。

+0

看看編輯,我添加了我的原始代碼。我知道我很迷惑,那是因爲我問這個問題:P – Quetool

1

好的,我解決了我的問題。問題是我在先前的UIVIewController的navigationController.view中添加了MBProgressHUD實例。

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

所以我覺得以前MBProgressHUD的「實例」還停留在navigationController.view一個沒有打破我的申請。

我希望我解釋一下自己,希望對未來的人有用!