tags:

views:

300

answers:

3

I've been using an NSTimer successfully, but am now having trouble with it. Undoubtably something stupid. Appreciate another set of eyes. Running the debugger, I see that applicationDidFinishLaunching is called, but trigger is never called.

-(void) trigger:(NSTimer *) theTimer{
    NSLog(@"timer fired");
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    nst = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(trigger) userInfo:nil repeats:YES];

    [window makeKeyAndVisible];
}
+1  A: 

The selector you're giving the timer, trigger, indicates that it should call a method that takes no parameter. Either change your timer-fired method to

 - (void)trigger
 {
      // look at me, I don't take any parameters
      NSLog(@"timer fired");
 }

or change your initial timer call to use @selector(trigger:).

Noah Witherspoon
The timer callback must have one argument so changing the method name to "trigger" is not going to work
@jib - No that is not true. The timer callback works fine without the NSTimer argument. I do that all the time.
St3fan
+4  A: 

The selector must have the following signature:

- (void)timerFireMethod:(NSTimer*)theTimer

so you need

@selector(trigger:)

--edit--

Maybe you are doing this somewhere else, but in the code you included you do not actually start the timer. You have to add it to a NSRunLoop before it can trigger any events at all.

 [[NSRunLoop currentRunLoop] addTimer:nst forMode:NSDefaultRunLoopMode];

If I read the examples correctly. I've only used the one the init method that automatically adds it to the current NSRunLoop. You really should look at the developer docs that someone included in the comments to my post.

willcodejavaforfood
bleh too slow again, sorry noah :)
willcodejavaforfood
+1 Here is the relevant documentation: http://developer.apple.com/mac/library/documentation/cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/occ/clm/NSTimer/timerWithTimeInterval:target:selector:userInfo:repeats:
e.James
to clarify:- : is part of the method name. Infact : is a totally valid method name for a method with one argument.
+2  A: 

Two things:

1) as others say, the method should have the following signature..

-(void) trigger:(NSTimer *) theTimer;

and you make the timer thus:

nst = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(trigger:) userInfo:nil repeats:YES];

2) merely creating the timer does not run it. As the documentation says:

You must add the new timer to a run loop, using addTimer:forMode:. Then, after seconds have elapsed, the timer fires, invoking invocation. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)

Here's a piece of real functioning code that you can model after. The timer creation is the same as yours, but it also adds it to runloop the right way.

[[NSRunLoop currentRunLoop] addTimer:
     [NSTimer timerWithTimeInterval:0.1
                             target:self
                           selector:@selector(someSelector:)
                           userInfo:nil
                            repeats:NO]
                                 forMode:NSDefaultRunLoopMode];
Jaanus
+1 this is correct. You can also use the `scheduledTimer...` convenience methods that will add the timer to the run loop for you.
Dave DeLong