Ja sie kiedyś tym bawiłem i o ile pamiętam działało dobrze:
Taki przykładowy kod:
Kod:
@implementation TimerTest2
- (void) start{
NSTimer *timer;
timer = [[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(onTimer)
userInfo:nil
repeats:YES] autorelease];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void) onTimer{
NSLog(@"Tick!");
}
@end
I drugi:
Kod:
@implementation TimerTest
-(void) startTimer
{
NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:nil]; //Create a new thread
[timerThread start]; //start the thread
}
//the thread starts by sending this message
-(void) startTimerThread
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
[NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: @selector(timerTick:)
userInfo: nil
repeats: YES];
[runLoop run];
[pool drain];
}
- (void)timerTick:(NSTimer *)timer
{
NSString *str = [NSString stringWithString:@"Tick!"];
NSLog(@"%@", str);
//NSString *str2 = [NSString stringWithContentsOfFile:@"/Users/maciek/Desktop/1.txt"];
}
Może coś z tego Ci się przyda.
Acha, i potem w main() mam takie coś:
Kod:
[[NSRunLoop currentRunLoop] run]
Tylko że ja jestem zupełnie początkujący w obj-c i tak się tylko bawię tym na razie