1

I have:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setNeedsStatusBarAppearanceUpdate];
    NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(setCurrentTime:)  userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    [timer fire];

}
-(void)setCurrentTime{
    NSLog(@"TEST");
    dispatch_async(dispatch_get_main_queue(), ^{
        NSDate *currentDate = [[NSDate alloc] init];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        [currentTime setText:[dateFormatter stringFromDate:currentDate]];
    });
}

But nothing get called.

40pro
  • 1,283
  • 2
  • 15
  • 23

1 Answers1

7

You're calling the wrong selector. Your "setCurrentTime" implementation doesn't take any parameter (e.g. to be properly messaged or called, you should use "selector:@selector(setCurrentTime)".

Now, if you look at Apple's documentation for [NSTimer scheduledTimerWitTimeInterval: target: selector: userInfo: repeats:], Apple says your method should have this signature:

- (void)setCurrentTime: (NSTimer *) timer

Which means your function would look something like this:

-(void)setCurrentTime: (NSTimer *) timer
{
    NSLog(@"TEST");
    dispatch_async(dispatch_get_main_queue(), ^{
        NSDate *currentDate = [[NSDate alloc] init];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        [currentTime setText:[dateFormatter stringFromDate:currentDate]];
    });
}

and be called like this:

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.25 
                   target:self 
                 selector:@selector(setCurrentTime:)  
                 userInfo:nil 
                  repeats:YES];
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • In some examples, like this one- --> http://stackoverflow.com/questions/1995825/nstimer-not-firing-when-runloop-is-blocked/1997018#1997018 . they have the colon. why? – 40pro Oct 07 '13 at 05:47
  • 1
    Because timer call method which takes one parameter. – Tomasz Szulc Oct 07 '13 at 05:47
  • Isn't it better to add the `NSTimer` argument to `setCurrentTime` so the signature is correct and it's what NSTimer expects? – trojanfoe Oct 07 '13 at 05:52
  • but if my NSTimer just updates the current time why would it need the NSTimer – 40pro Oct 07 '13 at 05:53
  • @stackplasm Apple's documentation says what kind of *method signature* the NSTimer creation methods are looking to work with. They expect a "`NSTimer *`" parameter, and if you wanted to pass along useful information to your method, you could use the NSTimer object's ["`userInfo`" dictionary](https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/occ/instm/NSTimer/userInfo) to do that. – Michael Dautermann Oct 07 '13 at 05:56
  • 1
    @stackplasm It's passed to the "trigger" method so it could stop the timer if it wanted. I don't understand what you mean by "NSTimer just updates the current time" though. Nothing inside a computer "updates time" and telling you what does is well beyond my comprehension. – trojanfoe Oct 07 '13 at 05:57