我想做一个“可重用”的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错误!

发布于 2015-12-03 21:00:44
好吧我解决了我的问题。问题是我在以前的MBProgressHUD中在navigationController.view中添加了一个UIVIewController实例。
HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];因此,我认为以前MBProgressHUD的“实例”仍然停留在navigationController.view上,这确实破坏了我的应用程序。
我希望我解释自己,我希望这将是有用的,在未来的人!
发布于 2015-12-03 19:00:22
[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中加入它呢?
我想你可能被MBProgressHUD的用法弄糊涂了。
https://stackoverflow.com/questions/34072843
复制相似问题