2012-06-27 6 views
5

Ho iniziato a giocare con UIProgressView in iOS5, ma in realtà non ha avuto fortuna. Ho problemi con l'aggiornamento della vista. Ho impostato delle azioni sequenziali, dopo che ho aggiornato i progressi. Il problema è che la vista sullo stato di avanzamento non viene aggiornata poco a poco ma solo dopo che tutti sono stati completati. E 'più o meno così:UIProgressView non si sta aggiornando?

float cnt = 0.2; 
for (Photo *photo in [Photo photos]) { 
    [photos addObject:[photo createJSON]]; 
    [progressBar setProgress:cnt animated:YES]; 
    cnt += 0.2; 
} 

messaggi Navigando overflow dello stack, ho trovato come questi - setProgress is no longer updating UIProgressView since iOS 5, implicando in modo che questo funzioni, ho bisogno di eseguire un thread separato.

Vorrei chiarire questo, ho davvero bisogno di thread separati per UIProgressView per funzionare correttamente?

risposta

18

Sì l'intero scopo della vista di avanzamento è per la filettatura.

Se stai eseguendo quel ciclo sul thread principale stai bloccando l'interfaccia utente. Se blocchi l'interfaccia utente, gli utenti possono interagire e l'interfaccia utente non può aggiornarsi. Dovresti fare tutto il lavoro pesante sul thread in background e aggiornare l'interfaccia utente sul thread principale.

Heres un piccolo campione di

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self performSelectorInBackground:@selector(backgroundProcess) withObject:nil]; 

} 

- (void)backgroundProcess 
{  
    for (int i = 0; i < 500; i++) { 
     // Do Work... 

     // Update UI 
     [self performSelectorOnMainThread:@selector(setLoaderProgress:) withObject:[NSNumber numberWithFloat:i/500.0] waitUntilDone:NO]; 
    } 
} 

- (void)setLoaderProgress:(NSNumber *)number 
{ 
    [progressView setProgress:number.floatValue animated:YES]; 
} 
2

Definire UIProgressView in .h di file:

IBOutlet UIProgressView *progress1; 

In .m File:

test=1.0; 
progress1.progress = 0.0; 
[self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO]; 

- (void)makeMyProgressBarMoving { 
    NSLog(@"test %f",test); 
    float actual = [progress1 progress]; 
    NSLog(@"actual %f",actual); 

    if (progress1.progress >1.0){ 
     progress1.progress = 0.0; 
     test=0.0; 
    } 

    NSLog(@"progress1.progress  %f",progress1.progress); 
    lbl4.text=[NSString stringWithFormat:@" %i %%",(int)((progress1.progress) * 100) ] ; 
    progress1.progress = test/100.00;//actual + ((float)recievedData/(float)xpectedTotalSize); 
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO]; 
    test++; 

} 
1

ho avuto problema simile. UIProgressView non stava aggiornando anche se ho fatto setProgress e anche provato setNeedsDisplay, ecc

float progress = (float)currentPlaybackTime/(float)totalPlaybackTime; 
[self.progressView setProgress:progress animated:YES]; 

ho avuto (int) prima di progresso nella parte setProgress. Se si chiama setProgress con int, non si aggiornerà come UISlider. Uno dovrebbe chiamarlo con valore float solo da 0 a 1.