Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cwebster

macrumors newbie
Original poster
Aug 31, 2010
2
0
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.

Code:
IBOutlet NSProgressIndicator *myProgressIndicator;
In terms of actions I have a push button that starts the calculation via:

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

Code:
IBOutlet for IBOutlet NSProgressIndicator *myProgressBar;

I then have a loop in the BatchConverter class

Code:
 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
 
You're blocking the main thread while you do your work, so it never gets a chance to draw. Try something like this:

Code:
- (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.
 
Or just make sure your progress bar is using threaded animation before you start the loop:
Code:
[myProgressBar setUsesThreadedAnimation:YES];
 
Thanks

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.