Question

I have an int which defines the objectAtIndex, which gives it to the string, which then displays it in the UILabel, HOWEVER, when I modify the array (add a word to it in this case) when I have the UILabel go through the array it only displays the odd ones. I have done an NSLog and found out that it DOES PASS EVERY SINGLE THING to the string, but the Label isn't displaying every single one, it only does it the first time, then I modify the array, and it only does odd

edit: the code:

- (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"];
    }
}

All of this is being controlled by an NSTimer, which sets it off based on a slider value. Also i is set to 0 in the IBAction

The IBAction code:

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];   

EDIT:

I fixed it! What I did was call this after i reached the count

 -(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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top