1

我有一个标签,我想在重新编码触摸时在一段时间内使用多个显示器更新它的文本。我可以使用 performSelector 但它看起来很笨重......

- (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 秒执行一次方法,但我不明白如何在我的情况下使用它。我有的是..

- (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() ,但它只会延迟标签更新之前的时间,并使用最后一条消息进行更新。

4

2 回答 2

0

Add an NSTimer property

@property (nonatomic, strong) NSTimer *timer;

A function to start the timer:

-(void) startTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(timerTick:)
                                                userInfo:nil
                                                 repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    [self.timer fire];
}

Timer tick event to get current time:

//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer
{

    Date *currentTime = [NSDate date];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
    NSString *timeNow = [dateFormatter stringFromDate:value];;

    [self updateDisplay:timeNow];
}

Needs to update display on main thread:

-(void)updateDisplay:(NSString *)str
{
    //NSLog(@"update display: %@", str);
    [self performSelectorOnMainThread:@selector(setLabelText:)
                           withObject:str
                        waitUntilDone:YES];
    [self setNeedsDisplay];
}

Setting the label text :)

-(void) setLabelText:(NSString *)labelText
{
    [self.label setText:labelText];
}
于 2013-09-29T17:56:15.263 回答
0

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

@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 时,数组将再次初始化:

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会重新归零,因此稳定安全。

于 2013-09-29T17:57:53.893 回答