The method that my NSButton calls, calls another method, via an NSTimer. Since any code in the method that calls the NSTimer will execute before the NSTimer is finished, I would like the same function that invalidates the NSTimer which is in the method that is called by the NSTimer to be able to set the NSButton state back to 0. I know that sounds terribly confusing so here is the code:
As you can see, once the sequence is finished, the NSButton will still be at the state that will call the [timer invalidate]; when pressed, crashing the program. Is there a way to get the -changeText method to change the state of the button when it invalidates the timer?
Code:
- (IBAction)start:(id)sender
{
if ([sender state] == 1) {
stringData = [[NSString alloc] initWithString:@"Text to be scrolling :)"];
mutableString = [[NSMutableString alloc] initWithString:@""];
x = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(changeText)
userInfo:nil
repeats:YES];
} else {
[timer invalidate];
}
}
- (void)changeText
{
theChar = [stringData characterAtIndex:x];
intermediateMutableString = [NSMutableString stringWithCharacters:&theChar length:1];
[mutableString appendString:intermediateMutableString];
[textField1 setStringValue: mutableString];
x++;
if ([mutableString length] == [stringData length]) {
[timer invalidate];
}
}