Take the 2-minute tour ×

First post and first iPhone app in the making here, so please excuse the any n00b mistakes. I'm having trouble activating the alarm. I have a Datepicker and a button as the crux of the code. Here's the .h file

@interface Alarm : UIViewController

@property (retain, nonatomic) NSDate *alarm;
@property (retain, nonatomic) IBOutlet UIButton *sleepButton;
@property (retain, nonatomic) NSTimer *timer;
@property (retain, nonatomic) IBOutlet UIDatePicker *timePicker;

- (IBAction)startAlarm:(id)sender;
- (void)checkAlarm;


And here is the .m file.

- (IBAction)startAlarm:(id)sender
    self.alarm = [self.timePicker date];

self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(checkAlarm) userInfo:nil repeats:YES];

- (void)checkAlarm
    // alarm is reached
    if ([NSDate date] == self.alarm)
         // rest of code goes here

I also have no idea how to make sound and make it repeat itself, so if I can get some guidance on how to do that, that would be great. Thanks!

share|improve this question
Isn't this functionality built into the iOS clock app? –  Richard J. Ross III Dec 6 '11 at 16:33
add comment

1 Answer

up vote 0 down vote accepted

The problem is here:

if ([NSDate date] == self.alarm)

Dates have millisecond resolution, and your timer only fires once per second - the chances that they align perfectly is rather slim.

Instead you should check:

if ([[NSDate date] compare: self.alarm] == NSOrderedDescending)

to check if the alarm date was passed.

share|improve this answer
add comment

Your Answer


