문제

I have two view controllers, both connected via Segue and using Storyboard.

In view controller 1 I have an NSTimer counting up and updating a UILabel.

When I switch to view controller 2 and back to 1 the uilabel is no longer updated.

Here is some code:

headerfile
NSString *timerTicksForCounter;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self updateTimerLabel];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self updateTimerLabel];
}

- (void) startLastConUpdater
{
    lastCTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                 target:self
                                               selector:@selector(updateTimer)
                                               userInfo:nil
                                                repeats:YES];
}

-(void) updateTimerLabel
{
    NSLog(@"timer: %@", timerTicksForCounter);
    if (timerTicksForCounter) {
        NSLog(@"timer not null");
        mainTimerLabel.text = timerTicksForCounter;
    }

}

- (void)updateTimer
{

    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:stopDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss"];

    timerTicksForCounter = [dateFormatter stringFromDate:timerDate];
    [self updateTimerLabel];
}
도움이 되었습니까?

해결책

What do you mean it's no longer updated ? Does this mean you lose what was displayed before switching or it doesn't update anymore. If it's not updating anymore it's because you don't start the timer in the appropriate method. You could do something like :

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self startLastConUpdater];
}

This should solve both issues I mentioned above.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top