views:

182

answers:

2

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

A: 

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.

Ben Gottlieb
nope, only one thing is calling this
Matt S.
+1  A: 

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.

luvieere
in the IBAction I have [timer invalidate]; as the first thing it does. is there another way to stop a timer?
Matt S.
Can you paste the whole timer code, so we could see if there are any problems with it? This way I could run it and try to figure out the actual problem.
luvieere
see the original post
Matt S.
I made your answer the answer because after reading it I got the inspiration for the fix
Matt S.
I'm curious what was wrong... I tried to replicate your bug but your code worked fine for me.
luvieere
I'm not sure either, but by adding that one little thing it now works like a dream!
Matt S.