Pregunta

I have a uitextlabel that is updated using an nstimer.

When I switch to another view controller (storyboard segue) and back again the text label is no longer updated (returns to default text), even though the timer continues to run.

The timer is writing a value to the uitextlabel which stops working after switching.

NOTE: the updateTimerLabel method continues to output the correct info but the label is not updated.

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];
}
¿Fue útil?

Solución

update your textField text in

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

}

First Declare

NSTimer * countdownTimer;
NSUInteger remainingTicks;

- (void)viewDidLoad
{
    [super viewDidLoad];
    remainingTicks = 60;
    [self updateLabel];

    countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(handleTimerTick) userInfo: nil repeats: YES];
}

-(void)handleTimerTick
{
    remainingTicks--;
    [self updateLabel];

    if (remainingTicks <= 0) {
        [countdownTimer invalidate];
        countdownTimer = nil;
    }
}

-(void)updateLabel
{
    timeLabel.text = [[NSNumber numberWithUnsignedInt: remainingTicks] stringValue];
}

Otros consejos

    //sent notification


        [[NSNotificationCenter defaultCenter] removeObserver:self];


    **//Get (Retrive) Notification**


         [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(receiveTestNotification:) 
                name:@"TestNotification"
                object:nil];

   //Get notification method. 

        - (void) receiveTestNotification:(NSNotification *) notification
        {
            // [notification name] should always be @"TestNotification"
            // unless you use this method for observation of other notifications
            // as well.

            if ([[notification name] isEqualToString:@"TestNotification"])
                NSLog (@"Successfully received the test notification!");

        **//Here write your code for update textfield**

        }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top