gpt4 book ai didi

ios - 切换 View Controller 后 uitextfield 不更新

转载 作者:行者123 更新时间:2023-11-28 22:07:45 24 4
gpt4 key购买 nike

我有一个使用 nstimer 更新的 uitextlabel。

当我切换到另一个 View Controller ( 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];
}

最佳答案

更新您的 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];
}

关于ios - 切换 View Controller 后 uitextfield 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23540761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com