Hi i’m trying to show a custom progress bar in my iPhone app for that i’m writing a method to increment the progress bar value and once it’s value becomes 100% then i need to invalidate my timer and i need to stop this recursion and show next viewController.
My code snippet is something like below,
-(void)progressNextValue
{
progressValue += 1.0f;
if(progressValue >= progress.maxValue){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"end" message:@"TimeOut!!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
NSLog(@"Time Out!!!!");
[mytimer invalidate];
Second *sec = [[Second alloc] initWithNibName:@"Second" bundle:nil];
[self.view addSubview:sec.view];
}
progress.currentValue = progressValue;
mytimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(progressNextValue) userInfo:nil repeats:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
progress.maxValue = 100.0f;
[self progressNextValue];
}
Here even if my progressValue = progress.maxValue, mytimer is not getting invalidated.
Any help is appreciated in advance.
You are setting your timer each time the method is run regardless. Add a return statement, or place the timer instantiation in an else statement.
For example: