【问题标题】:Reusable MBProgressHUD可重复使用的 MBProgressHUD
【发布时间】: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


【解决方案1】:

好的,我解决了我的问题。问题是我在以前的 UIVIewController 的 navigationController.view 中添加了一个 MBProgressHUD 实例。

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

所以我认为 MBProgressHUD 之前的“实例”仍然停留在 navigationController.view 上,确实破坏了我的应用程序。

我希望我自己解释一下,希望以后对某人有用!

【讨论】:

    【解决方案2】:

    [HUD hide:YES] 用动画隐藏界面。 [HUD show:NO] 显示没有动画的拥抱。

    这个 API 的命名有点奇怪,应该是这样的:[HUD hideWithAnimated:][HUD showWithAnimated:]

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

    在functionTwo中有一个视图吗?如果你NSLog(@"view: %@", self.navigationController.view) 会发生什么?

    另外,如果你重新分配它,你为什么要在viewDidLoad 中初始化你的HUD?

    我想你可能对 MBProgressHUD 的使用感到困惑。

    【讨论】:

    • 查看编辑,我添加了我的原始代码。我知道我很困惑,那是因为我在问这个问题:P
    猜你喜欢
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多