Quote:
Originally Posted by DeeJay_CRO
Hello!
Thanks for the answers. I did it in the following way:
Code:
- (void) thisMethodIsTrigered {
NSLog(@"setMessage should be triggered now");
NSTimer *messageTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
target: self
selector:@selector(setMessageText:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:messageTimer forMode:NSDefaultRunLoopMode];
}
- (void)setMessageText {
NSLog(@"setMessage triggered !!!");
}
In the console, I see only:
2011-05-27 13:15:48.924 Zdravo_Budi[1100:207] setMessage should be triggered now
I expected that setMessage triggered !!! will be written every 3 seconds.
Any idea why this happens?
Thanks a lot!
|
That second line doesn't belong there (I marked it in bold red.) The method scheduledTimerWithTimeInterval creates a timer and adds to the default run loop all in one step. I don't know what will happen when you then add the timer again, but it probably won't be good.
Dany's pointed out the other problem that your setMessageText method needs to have the timer as a parameter. Methods with different numbers of parameters (and different parameter names) are actually different methods in Objective C.
The selector for a method with one parameter looks like this:
Code:
@selector(setMessageText:)
(note the colon after the method name)
The selector for a method with no parameters looks like this:
Code:
@selector(setMessageText)
(note the
lack of a colon after the method name)
The selector for a timer method needs to take one and only one parameter: the timer itself.