Ios 可重复使用
我想制作一个“可重复使用”的MBProgressHUD。例如,我有一个类似这样的代码Ios 可重复使用,ios,objective-c,mbprogresshud,Ios,Objective C,Mbprogresshud,我想制作一个“可重复使用”的MBProgressHUD。例如,我有一个类似这样的代码 - (void)viewDidLoad { HUD = [[MBProgressHUD alloc] init]; HUD.delegate = self; [self functionOne]; } - (void)functionOne { //turn on HUD HUD = [MBProgressHUD showHUDAddedTo:self.navigat
- (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];
}
];
}
问题在于//再次打开相同的HUD时代码崩溃,出现错误:
-[MBProgressHUD initWithView:]中的断言失败-由于未捕获的异常“NSInternalInconsistencyException”,正在终止应用程序,原因是:“视图不能为零。”
我不知道为什么,特别是如果我在viewDidLoad中分配并初始化它
另一件事是我不理解[HUD隐藏:是]
和[HUD显示:否]
编辑:这是我的原始代码
@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的行,我也删除了#导入和委托,但我仍然有那个错误!
[HUD隐藏:是]
使用动画隐藏HUD。
[HUD show:NO]
在没有动画的情况下显示拥抱
这个API的命名有点奇怪,它应该是这样的:[HUD hideWithAnimated:
,[HUD SHOWWITHANTINATED:
因此,您可能希望使用[HUD hide:YES]
。。而不是[HUD显示:否]
在功能二中,你有视野吗?如果您NSLog(@“view:%@”,self.navigationController.view)
,会发生什么情况
另外,如果在之后重新分配HUD,为什么要在viewDidLoad
中初始化HUD
我想您可能会对MBProgressHUD的使用感到困惑。好的,我解决了我的问题。问题是我在以前的UIVIewController中的navigationController.view中添加了一个MBProgressHUD实例
HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
因此,我认为MBProgressHUD的前一个“实例”仍然停留在navigationController.view上,它确实破坏了我的应用程序
我希望我解释了我自己,我希望这将是有用的人在未来 您正在尝试在导航视图中添加进度吗?我想你的问题来自于是的,我在导航视图中添加了无处不在的内容。问题是第二场演出。你说在哪里添加它?我使用[MBProgressHUD hideHUDForView:theView animated:YES]代码>而不是show:方法。你能试试吗?不,同样的事情也会发生。看编辑,我添加了我的原始代码。我知道我很困惑,那是因为我问了一个问题:P