2012-06-27 37 views
5

Tôi đã bắt đầu chơi với UIProgressView trong iOS5, nhưng havent thực sự đã có may mắn với nó. Tôi gặp sự cố khi cập nhật chế độ xem. Tôi đã thiết lập các hành động tuần tự, sau mỗi lần tôi cập nhật tiến trình. Vấn đề là, tiến trình xem không được cập nhật từng chút một mà chỉ sau khi tất cả đã kết thúc. Nó đi một cái gì đó như thế này:UIProgressView không cập nhật?

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

Duyệt stack overflow, tôi đã tìm thấy bài viết như thế này - setProgress is no longer updating UIProgressView since iOS 5, ngụ ý để cho tiện làm việc, tôi cần phải chạy một thread riêng biệt.

Tôi muốn làm rõ điều này, tôi có thực sự cần chuỗi riêng cho UIProgressView để hoạt động bình thường không?

Trả lời

18

Có toàn bộ mục đích của chế độ xem tiến trình là cho luồng.

Nếu bạn đang chạy vòng lặp đó trên chuỗi chính bạn đang chặn giao diện người dùng. Nếu bạn chặn giao diện người dùng thì người dùng có thể tương tác và giao diện người dùng không thể cập nhật. YOu nên thực hiện tất cả việc nâng hạng nặng trên luồng nền và cập nhật giao diện người dùng trên Chủ đề chính.

Heres một mẫu nhỏ

- (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

Xác định UIProgressView trong .h file:

IBOutlet UIProgressView *progress1; 

Trong .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

Tôi có vấn đề tương tự. UIProgressView không cập nhật mặc dù tôi đã đặt setProgress và thậm chí đã thử setNeedsDisplay, v.v.

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

Tôi đã có (int) trước khi tiến hành trong phần setProgress. Nếu bạn gọi setProgress với int, nó sẽ không cập nhật như UISlider. Người ta nên gọi nó bằng giá trị phao chỉ từ 0 đến 1.

Các vấn đề liên quan