while loop with sleep(): logging works, but not UI events
while loop with sleep(): logging works, but not UI events
- Subject: while loop with sleep(): logging works, but not UI events
- From: Daniel Richman <email@hidden>
- Date: Fri, 27 Jun 2008 20:55:45 -0700
Hi All,
I'm trying to program a simple timer app: you enter a number of seconds,
and it updates a text field every second with the number of secs
remaining. The problem is that I'm not able to do anything with the UI
while this is going on. Here's my code:
- (IBAction)startTimer:(id)sender
{
[startButton setState:NSOffState];
int timeInSeconds = [((NSNumber *)[inTextField objectValue]) intValue];
while (timeInSeconds > 0) {
[outTextField setStringValue:[NSString stringWithFormat:@"%d
more second(s) to go", timeInSeconds]];
sleep(1);
timeInSeconds--;
}
[outTextField setStringValue:@"Finished timing"];
NSBeep();
}
What's happening is that if I enter a number and then press 'start',
'start' stays in the 'pressed' position until the loop finishes, after
which it returns to the normal position. My guess is that this is
stopping any other UI events from occurring. This would also explain why
I'm able to log a message each time the loop iterates.
But I'm still not sure why the button is staying pressed. Thoughts, anyone?
Daniel
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden