您好我有基于导航的应用程序,其中一个 View 中有一个计时器。 (查看方式:A、B 和 C)
当我启动计时器时,我在 C 中有计时器,它工作正常,但是当我推回任何 View 并再次来到 View C 时,它没有显示更新的值。
这是我的代码。
应用代理
-(int)updateTimer
{
timer_value--;
return timer_value;
}
查看“C”代码
- (IBAction)buttonClick:(id)sender {
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
}
-(void)update
{
MFAppDelegate *appDelegate = (MFAppDelegate *) [[UIApplication sharedApplication]delegate];
int Time=[appDelegate updateTimer];
int minute=Time/60;
int second=Time-(minute*60);
NSString *strValue=[NSString stringWithFormat:@"%d:%d",minute,second];
NSLog(@"%@",self.lbl_timer.text);
[self.lbl_timer setText:strValue];
}
每次都调用更新函数,并且标签文本的 NSlog 显示正确。
有什么我做错了吗?请帮帮我。
最佳答案
在你的课上
-(void)update
{
MFAppDelegate *appDelegate = (MFAppDelegate *) [[UIApplication sharedApplication]delegate];
int Time=[appDelegate updateTimer];
int minute=Time/60;
int second=Time-(minute*60);
NSString *strValue=[NSString stringWithFormat:@"%d:%d",minute,second];
NSLog(@"%@",self.lbl_timer.text);
[[NSNotificationCenter defaultCenter] postNotificationName:@"DoUpdateLabel" object:strValue userInfo:nil];
}
- (void) updateLabel:(NSString *)string
{
yourLabel.text = string;
}
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLabel:) name:@"DoUpdateLabel" object:nil];
}
关于ios - UILabel 文本没有更新 NSTimer objective-c ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16686677/