切换视图控制器后uitextfield不更新

时间:2014-05-08 11:42:26

标签: ios ipad

我有一个使用nstimer更新的uitext标签。

当我切换到另一个视图控制器(storyboard segue)并再次返回时,文本标签不再更新(返回默认文本),即使计时器继续运行。

计时器正在向uitextlabel写一个值,切换后停止工作。

注意:updateTimerLabel方法继续输出正确的信息,但标签未更新。

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];
}

2 个答案:

答案 0 :(得分:2)

更新

中的textField文字
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self updateLabel];

}

首先声明

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];
}

答案 1 :(得分:0)

    //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**

        }