cant get Progress Bar to work

Discussion in 'Mac Programming' started by cwebster, Aug 31, 2010.

  1. macrumors newbie

    #1
    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
     
  2. macrumors 68030

    Catfish_Man

    #2
    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.
     
  3. Moderator emeritus

    kainjow

    #3
    Or just make sure your progress bar is using threaded animation before you start the loop:
    Code:
    [myProgressBar setUsesThreadedAnimation:YES];
     
  4. macrumors newbie

    #4
    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
     

Share This Page