To allow the execution cycle to run between messages, use NSTimeror delay the execution. Here's the last one:
- (IBAction) start:(id)sender {
[self performSelector:@selector(updateTextFieldWithNumber:) withObject:[NSNumber numberWithInt:0] afterDelay:1.0];
}
- (void) updateTextFieldWithNumber:(NSNumber *)num {
int i = [num intValue];
[outputField setIntValue:i];
if (i < 10)
[self performSelector:@selector(updateTextFieldWithNumber:) withObject:[NSNumber numberWithInt:++i] afterDelay:1.0];
}
Here is one timer based solution. It may be easier for you to follow. You can set the value of the text field from the text field:
@interface TestNums: NSObject
{
IBOutlet NSTextField *outputField;
NSTimer *timer;
int currentNumber;
}
@end
@implementation TestNums
- (IBAction) start:(id)sender {
timer = [[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTextField:)
userInfo:nil
repeats:YES] retain];
currentNumber = 0;
[outputField setIntValue:currentNumber];
}
- (void) updateTextField:(NSTimer *)timer {
[outputField setIntValue:++currentNumber];
}
@end
( ) , . Interface Builder ( , ⌘4, currentNumber ).
@interface TestNums: NSObject
{
NSTimer *timer;
int currentNumber;
}
@property int currentNumber;
@end
@implementation TestNums
@synthesize currentNumber;
- (IBAction) start:(id)sender {
timer = [[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTextField:)
userInfo:nil
repeats:YES] retain];
self.currentNumber = 0;
}
- (void) updateTextField:(NSTimer *)timer {
self.currentNumber = ++currentNumber;
}
@end
, , :
- . ( , , .)
- , IB. - TestNums.