如何使用NSTimer更新标签? [英] How to use NSTimer to update a label?

查看:37
本文介绍了如何使用NSTimer更新标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标签,当触摸被重新编码时,我希望在一段时间内使用多个显示来更新其文本.我可以使用performSelector,但看起来笨重...

I have a label that I want to update the text of it with multiple displays over a period of time when a touch is recoded. I can use performSelector but it seems bulky...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self changeTextForLabel:@"A text (1)"]; // When touch begins the display is changed
[self performSelector:@selector(changeTextForLabel:) withObject:@"Another text (2)" afterDelay:1];               // After 1 second update to this
[self performSelector:@selector(changeTextForLabel:) withObject:@"And another text (3)" afterDelay:2];           // after 2 seconds update to this
[self performSelector:@selector(changeTextForLabel:) withObject:@"And even another text (4)" afterDelay:3];      // After 3 seconds update to this
[self performSelector:@selector(changeTextForLabel:) withObject:@"And yes even another text (5)" afterDelay:3]; }  

我听说人们谈论使用计时器每x秒执行一个方法,但是我不了解如何针对我的情况使用它.我所拥有的是..

I heard people talk about using timers to execute a method every x seconds, but i dont understand how to use it for my situation. What i have is..

- (void)updateLabel:(NSTimer *)theTimer {
[self changeTextForLabel:@"A text (1)"];
[self changeTextForLabel:@"Another text (2)"];
[self changeTextForLabel:@"And another text (3)"];
[self changeTextForLabel:@"And even another text (4)"];
[self changeTextForLabel:@"And yes even another text (5)"];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES]; }

但是这仅显示最后一条消息.但是我希望它在1秒后一个接一个地显示.我试图在每条消息之间使用pause()或sleep(),但是它只会延迟标签更新之前的时间,并且会随着最后一条消息的更新而延迟.

But this only displays the very last message.. but i want it to display one after another after 1 second. I tried to use pause() or sleep() in between every message but it only delays the time before the the label is updated and it gets updated with the last message.

推荐答案

这是因为您无需等待一秒钟即可更新每个标签.尝试这样的事情:

This because you're updating each label without waiting one second. Try something like this:

@property (nonatomic) NSArray* titles;

...

- (NSArray*) titles {
    if(!_titles)
        _titles= @[@"A text (1)", @"Another text (2)", @"And another text (3)", @"And even another text (4)", @"And yes even another text (5)"];
    return _titles;
}

- (void)updateLabel:(NSTimer *)theTimer {
    if(self.titles.count)
    {
        [self changeTextForLabel: self.titles[0] ];
        // Alternatively use a NSMutableArray
        self.titles= [self.titles subarrayWithRange: NSMakeRange(1,self.titles.count-1) ];
    }
    else { // Invalidate the timer
    }
}

如果要重新初始化数组以再次显示所有标题,将 titles 设置为 nil 就足够了,以便下次使用调用getter,数组将再次初始化:

If you want to re-initialize the array to display all the titles again, it's enough that you set titles to nil, so that the next time you'll call the getter, the array will be initialized again:

self.titles= nil;  // Now _titles is nil
self.titles; // Not it's the full array with all the 5 objects

替代

- (void)updateLabel:(NSTimer *)theTimer {
    static unsigned int count=0;
    [self changeTextForLabel: self.titles[count++ % self.titles.count] ];
    if(count==5) {
        // Invalidate the timer
    }
}

PS:使用这最后一个代码,您无需重置阵列.当 count 溢出时,它将重新开始为零,因此它是稳定且安全的.

PS: With this last code you don't need to reset the array. When count overflows it will restart to zero, so it's stable and safe.

这篇关于如何使用NSTimer更新标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆