gpt4 book ai didi

ios - 可重复使用的 MBProgressHUD

转载 作者:行者123 更新时间:2023-11-29 12:09:02 25 4
gpt4 key购买 nike

我想制作一个“可重用”的 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];
}
];
}

问题是 //再次打开 THE SAME HUD 时代码崩溃,错误为:

Assertion failure in -[MBProgressHUD initWithView:] - Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'View must not be 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 和委托(delegate),但我仍然有那个该死的错误! enter image description here

最佳答案

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

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

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

我希望我自己解释了,我希望它对将来的人有用!

关于ios - 可重复使用的 MBProgressHUD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34072843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com