我想製作一個「可重用」的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錯誤!
您是否試圖在導航視圖中添加進度?我認爲你的問題來自 – FabKremer
是的,我在導航視圖中無處不在。第二個節目出現問題。你在哪裏添加它? – Quetool
我使用'[MBProgressHUD hideHUDForView:theView animated:YES];'而不是show:方法。你可以試試嗎? – FabKremer