linking a NSTimer to a NSProgressIndicator

2.3k views Asked by At

I need to make an NSTimer that will tell another class to fire in 10 seconds, and I would also like to use it to animate a determinate NSProgressIndicator. I also need to know how to change my indeterminate progress indicator to determinate. I was able to find a Apple doc on this, however a more in-depth explanation would help.

2

There are 2 answers

0
Justin Boo On

From indeterminate to determinate NSProgressIndicator You can change in IB. Select Your progressIndicator and go to Attributes inspector and uncheck Indeterminate checkbox like this:

Screenshot

Or it can be done programatically:

[progressIndicatorOutlet setIndeterminate:NO];

Note: progressIndicatorOutlet is Your NSProgressIndicator outlet, so don't forget to IBOutlet it.


Determinate NSProgressIndicator animation:

It's very simple just set and change value like this:

[progressIndicatorOutlet setDoubleValue:10];

Note: progressIndicatorOutlet is Your NSProgressIndicator outlet, so don't forget to IBOutlet it.


NSTimer:

Simple timer example:

//Place this where You want to call class after 10 sec. (for example: when button pressed)
//It will call class with name callAfter10sec after 10 sec. Place in there what You need to do. 

[NSTimer scheduledTimerWithTimeInterval:10.0
                                 target:self
                               selector:@selector(callAfter10sec:)
                               userInfo:nil
                                repeats:YES];

Don't forget to add class which I mentioned in comments like this:

-(void)callAfter10sec:(NSTimer *)time {
    // Do what You want here

}
0
PradeepKN On

Hope it helps.

Using the following way we can achieve what is expected

NSInteger progressValue;
NSTimer *timerObject;

progressValue = progressMaxValue;

timerObject = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(incrementProgressBar) userInfo:nil repeats:YES];

[timerObject fire];
[self.progressBar setMinValue:progressMinValue]; 
[self.progressBar setMaxValue:progressMaxValue];
[self.progressBar setDoubleValue:progressValue];

- (void)incrementProgressBar {
    // Increment the progress bar value by 1
    [self.progressBar incrementBy:1.0];
    progressValue--;
    [self.progressBar setDoubleValue:progressValue];
}