【发布时间】:2015-12-03 17:49:49
【问题描述】:
我想制作一个“可重复使用”的 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 上的代码崩溃并出现错误:
-[MBProgressHUD initWithView:] 中的断言失败 - 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“视图不得为 nil。”
我不知道为什么,特别是如果我在 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 的行,我还删除了 #import 和委托,但我仍然有那个 fu***ng 错误!
【问题讨论】:
-
您是否要在导航视图中添加进度?我认为您的问题来自
-
是的,我在导航视图中的任何地方都添加了。问题出现在第二场演出中。你说在哪里添加它?
-
我使用
[MBProgressHUD hideHUDForView:theView animated:YES];而不是 show: 方法。你可以试试吗? -
不,同样的事情发生了。
标签: ios objective-c mbprogresshud