PDA

View Full Version : cant get Progress Bar to work




cwebster
Aug 31, 2010, 04:39 PM
Hi,

I'm just starting out with objective-c and have written a small program that takes input from a text file and does some calculations on the data. I'd like a progress bar to update while the calculations are ongoing.

I have a main app window with a progress bar in it and a controller with a linked IBOutlet.


IBOutlet NSProgressIndicator *myProgressIndicator;

In terms of actions I have a push button that starts the calculation via:


- (IBAction)batch:(id)sender{
BatchConverterObj =[[BatchConverter alloc]init];
[BatchConverterObj setHdlFactor:[hdlFactorField doubleValue]];
[BatchConverterObj BatchConvert];
}

The calculation then takes place in another class(BatchConverter) which I've also linked an


IBOutlet for IBOutlet NSProgressIndicator *myProgressBar;


I then have a loop in the BatchConverter class


for (i = 0, count = [csvData count]; i < count; i = i + 1)
{
//start a progress bar
[myProgressBar setDoubleValue:100*i/(double)count];

But nothing appears to happen.

I've also tried putting the loop in the initial action and again nothing happens and having only one outlet in either class.

Thanks
Craig



Catfish_Man
Aug 31, 2010, 06:54 PM
You're blocking the main thread while you do your work, so it never gets a chance to draw. Try something like this:


- (void) processNextChunk {
//do one loop iteration of work
i++;
[myProgressBar setDoubleValue:100*i/(double)count];
[self performSelector:@selector(processNextChunk) afterDelay:0.0];
}


If that introduces too much overhead, you could do it in larger chunks than 1.

kainjow
Aug 31, 2010, 08:38 PM
Or just make sure your progress bar is using threaded animation before you start the loop:
[myProgressBar setUsesThreadedAnimation:YES];

cwebster
Sep 1, 2010, 05:25 PM
Thanks, got it to work if I do the code in the class that responds to the button press, but not when I transfer over to another class (if that makes any sense)

Cheers
Craig