ios - UILabel 文本没有更新 NSTimer objective-c ?

标签 ios objective-c timer nstimer

您好我有基于导航的应用程序,其中一个 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/

相关文章:

javascript - js如何杀死定时器

php - 用户停止输入后自动运行 js 检查用户名可用性是行不通的

javascript - 每次 setInterval 触发时都会发生 SyntaxError

ios - 如何检查代码是否在 UIView 的动画 block 中运行

objective-c - 多个协议(protocol)的语法

ios - 将下载的PDF存储到文档目录不起作用

iOS:请求访问相机

ios 滚动应该从主视图的顶部开始 - 而不是在 View 中的 tableview

ios - 使用 CocoaPods 和 Obj-C 桥接头

iphone - 以编程方式将数组复制到 Storyboard 中的新 View