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;
@end
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!
The problem is here:
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:
to check if the alarm date was passed.