2017-12-19 18 views
1

내 응용 프로그램이 처음으로 많은 양의 데이터를 다운로드하는 시나리오가 있으며 이는 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; 


    }); 
    } 
} 

업데이트 : 여기

내가 뭘했는지의 나는 당신은 인스턴스 변수를 만든 데이터

답변

1

를 저장하는 클래스에 의해 게시 된 알림을 수신하고있어 MBProgressHUDMBProgressHUD *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 HUDForView:self.view].progress = progress;으로 증가합니다. 당신이 진행을 숨기려면 당신이 HUD를 만드는 것으로 나타

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

당신이 MBProgressHUD에 대한 설명서에 따르면

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

진행 상황을'[hud setProgress : progress]'로 설정하려고 시도했습니다. 그러나 진행 상황은 업데이트되지 않습니다. – Prashant

+0

업데이트 내 대답은 메인 큐에 설정된 진도를 기쁘게 할 수있다 – Vinodh

0

를 사용할 수 있습니다 아래의 문제에 대한

수정입니다 틀리게. 그들이는 다음과 같다주는 예 ...

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 

// Set the bar determinate mode to show task progress. 
hud.mode = MBProgressHUDModeDeterminateHorizontalBar; 
hud.label.text = NSLocalizedString(@"Loading...", @"HUD loading title"); 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 
    // Do something... 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [MBProgressHUD hideHUDForView:self.view animated:YES]; 
    }); 
}); 

당신은 인스턴스를 생성하고 UIWindow

+0

이것은 내가 수평 막대를 필요로하기 때문이다. 'MBProgressHUDModeDeterminateHorizontalBar' – Prashant

+0

@Prashant 예제를 업데이트했습니다. 데모 프로젝트를 볼 가치가 있습니다. – Flexicoder

+0

감사합니다. 나는 그것을 시도 할 것이다. 나는 똑같은 노력을 해왔다. – Prashant

0

공식 문서는이처럼 나열에 부착되어

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
hud.mode = MBProgressHUDModeAnnularDeterminate; 
hud.label.text = @"Loading"; 
NSProgress *progress = [self doSomethingInBackgroundCompletion:^{ 
    [hud hideAnimated:YES]; 
}]; 
hud.progressObject = progress; 

하는 당신은 찾을 수 있습니다 여기에 : https://github.com/jdg/MBProgressHUD