내 응용 프로그램이 처음으로 많은 양의 데이터를 다운로드하는 시나리오가 있으며 이는 ViewController1
에서 발생합니다. 다른 클래스를 사용하여 데이터를 다운로드하고 다른 클래스를 사용하여 데이터를 저장합니다. 그래서 내 질문은 MBProgressHUD
개체의 진행 상황을 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;
});
}
}
업데이트 : 여기
내가 뭘했는지의 나는 당신은 인스턴스 변수를 만든 데이터
진행 상황을'[hud setProgress : progress]'로 설정하려고 시도했습니다. 그러나 진행 상황은 업데이트되지 않습니다. – Prashant
업데이트 내 대답은 메인 큐에 설정된 진도를 기쁘게 할 수있다 – Vinodh