Question

I have a UIScrollView which is being populated by UILabels which are being updated by NSTimers on each page which is being populated by an NSDictionary

for (id key in _infoDict) {

        NSTimer *releaseDateTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self
                                                              selector: @selector(calculateReleaseDate:) userInfo: dateInfo repeats: YES];
        [self.releaseDateTimer fire];

}

The UILabels which are being refreshed are all referenced by their tags which are based on which page they are on and which calendar unit.

-(void)calculateReleaseDate:(NSTimer*)theTimer {

        NSString *monthString = [[NSString alloc]initWithFormat:@"%i",[finalDate month]];
        NSString *dayString = [[NSString alloc]initWithFormat:@"%i",[finalDate day]];
        NSString *hourString = [[NSString alloc]initWithFormat:@"%i",[finalDate hour]];
        NSString *minutesString = [[NSString alloc]initWithFormat:@"%i",[finalDate minute]];
        NSString *secondsString = [[NSString alloc]initWithFormat:@"%i",[finalDate second]];

        UILabel *monthValue = (UILabel*)[self.scrollView viewWithTag:[monthTag intValue]];
        [monthValue setText:monthString];

        UILabel *dayValue = (UILabel*)[self.scrollView viewWithTag:[dayTag intValue]];
        [dayValue setText:dayString];

        UILabel *hourValue = (UILabel*)[self.scrollView viewWithTag:[hourTag intValue]];
        [hourValue setText:hourString];

        UILabel *minValue = (UILabel*)[self.scrollView viewWithTag:[minTag intValue]];
        [minValue setText:minutesString];

        UILabel *secValue = (UILabel*)[self.scrollView viewWithTag:[secTag intValue]];
        [secValue setText:secondsString];

}

This isn't the complete method but you get the idea. Now at some point I want to rebuild the entire page by removing all the UIScrollView subviews and then repopulating it.

This is where my problem is coming in is that my labels values are jumping between numbers. I can only assume that there are still instances of my previously created timers are still accessing these labels as well.

Is there a way to access the timers on the runloop and running [NSTimer invalidate]; on them or am I going about this all wrong.

Any input would be appreciated.

Était-ce utile?

La solution

You can add your NSTimer instances in a NSMutableArray in your for loop. Then, you can invalidate them if they are still valid in another for loop. You can use isValid method to check if NSTimer is still valid.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top