0

我有一个定义 objectAtIndex 的 int,它将它提供给字符串,然后在 UILabel 中显示它,但是,当我修改数组时(在这种情况下添加一个单词),当我让 UILabel 遍历数组时它只显示奇数。我做了一个 NSLog,发现它确实将每一件事都传递给了字符串,但是标签并没有显示每一个,它只是第一次这样做,然后我修改了数组,它只是奇怪

编辑:代码:

- (void) stuff {
    NSArray *indivwords = [sentence componentsSeparatedByString:@" "];
    count = [indivwords count]; //count=int
    if (i < count) {
        NSString *chunk = [indivwords objectAtIndex:i];
        [word setText:chunk];

        NSLog(chunk);
        i++;
    } else {
        word.textColor = [UIColor greenColor];
        [word setText:@"DONE"];
    }
}

所有这些都由 NSTimer 控制,它根据滑块值将其设置为关闭。i 在 IBAction 中也设置为 0

IBAction 代码:

word.textColor = [UIColor whiteColor];
[timer invalidate];
i = 0;

speedd = (1/speed.value)*60;



timer = [NSTimer scheduledTimerWithTimeInterval:(speedd) target:self selector:@selector(stuff) userInfo:nil repeats:YES];   

编辑:

我修好了它!我所做的就是在我达到计数后调用这个

 -(void)stoptimer
{
[timer invalidate];
timer = [NSTimer scheduledTimerWithTimeInterval:(.01) target:self selector:@selector(empty) userInfo:nil repeats:YES];  
}

empty is just a void with nothing in it, well, all I did was add a comment,

//don't look over here, nothing to see here, oh look at that

Somebody can close this if they want

4

2 回答 2

1

You said the time interval of your NSTimer is controlled by a slider. Perhaps you are starting a new timer instance every time you change the slider value, so after a change you would have two timers running. The first one updates the label and increments i. The second one comes right after the first and finds i's value incremented, so it displays the next chunk instead of the current one.

Count the instances of your timer - NSLog the timer in the timer callback and compare the results - it may be that you're firing more than one.

于 2009-11-27T10:49:41.187 回答
0

My guess is that you have two different actions calling this method, when you think there should be one. I'd put a break point in there, and see if it stops where you think it should.

于 2009-11-27T02:58:36.723 回答