gpt4 book ai didi

ios - MBProgressHUD 更新进度

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

我有一个场景,我的应用程序仅在第一次下载大量数据,并且这种情况发生在 ViewController1 中。我使用不同的类来下载数据,使用另一个类来保存数据。所以我的问题是,如何更新在 ViewController1 中创建的 MBProgressHUD 对象的进度以向用户显示进度?

我采用的方法是使用NSNotificationCenter 发送通知。我在保存数据的类中的方法 (13) 末尾发送通知。

这是我一直在做的:

//ViewController1.h
@interface ViewController1 ()
{
MBProgressHUD *hud;
float progress;
}
@end


//ViewController1.m
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"downloadComplete"
object:nil];

}
- (IBAction)sumitButtonDidClick:(id)sender {
hud = [[MBProgressHUD alloc] init];
hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
hud.label.text = NSLocalizedString(@"Please wait...", @"HUD preparing title");
hud.minSize = CGSizeMake(150.f, 100.f);

[hud showAnimated:YES];


double delayInSeconds = 1.0;

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
progress = 0.0f;

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
DownloadClass * obj1 = [[DownloadClass alloc] init];
[obj1 downloadData];
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});
}

- (void) receiveNotification:(NSNotification *) notification {

if ([[notification name] isEqualToString:@"downloadComplete"])
{
NSLog (@"Successfully received the download complete notification!");
progress += 7.7f;
//[hud setProgress:progress]; // won't work
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD HUDForView:self.view].progress = progress;


});
}
}

Update: I'm receiving the notifications posted by the class that saves data

最佳答案

您已经为 MBProgressHUD 创建了一个实例变量作为 MBProgressHUD *hud; 并且您开始在 sumitButtonDidClick 方法上取得进展,如下所示:

hud = [[MBProgressHUD alloc] initWithFrame:CGRectMake(0, -30, 320, 768) ];
hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
hud.label.text = NSLocalizedString(@"Please wait...", @"HUD preparing title");
hud.minSize = CGSizeMake(150.f, 100.f);
UIWindow *window=[[[UIApplication sharedApplication]delegate]window];

hud.center=window.center;

[window addSubview:hud];

[hud showAnimated:YES];

但是作为 MBProgressHUD 的类方法增加进度为 [MBProgressHUD HUDForView:self.view].progress = progress;.

问题的修复如下:

dispatch_async(dispatch_get_main_queue(), ^{
hud.progress = progress;
});

如果你想隐藏进度可以使用

 dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});

关于ios - MBProgressHUD 更新进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47884270/

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