Alex G
Alex G

Reputation: 2309

Making alarm clock with NSTimer

I just went through trying to make an alarm clock app with local notifications but that didn't do the trick because I needed an alert to appear instead of it going into notification centre in iOS 5+

So far I've been struggling greatly with nstimer and its functions so I was wondering if anyone could help out.

I want when the user selects a time (through UIDatePicker, only time) for an alert to be displayed at exactly this time. I have figured out how to get the time from UIDatePicker but I do not know how to properly set the firing function of nstimer. This is what I have attempted so far, if anyone could help... be much appreciated. Thank you

Example (it keeps going into the function every second opposed to a certain time I told it too... not what I want):

NSDate *timestamp;
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];

[comps setHour:2];
[comps setMinute:8];
timestamp = [[NSCalendar currentCalendar] dateFromComponents:comps];

NSTimer *f = [[NSTimer alloc] initWithFireDate:timestamp
                             interval:0
                               target:self
                             selector:@selector(test)
                             userInfo:nil repeats:YES];

NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:f forMode: NSDefaultRunLoopMode]; 

Upvotes: 3

Views: 3225

Answers (1)

Janak Nirmal
Janak Nirmal

Reputation: 22726

As @Nate said change

NSTimer *f = [[NSTimer alloc] initWithFireDate:timestamp
                             interval:0
                               target:self
                             selector:@selector(test)
                             userInfo:nil repeats:YES];

To

NSTimer *f = [[NSTimer alloc] initWithFireDate:timestamp
                             interval:0
                               target:self
                             selector:@selector(test)
                             userInfo:nil repeats:NO]; //<-- Change here

Upvotes: 2

Related Questions