I am having trouble creating alarm app. all I have is Datepicker, label, button.
@interface app6ViewController : UIViewController {
UILabel *dateLabel;
UIDatePicker *datePicker;
NSDate *alarm;
}
-(IBAction)getSelection;
-(开发者_开发技巧IBAction)checkAlarm;
@property(nonatomic,retain) IBOutlet UILabel *dateLabel;
@property(nonatomic,retain) IBOutlet UIDatePicker *datePicker;
@end
and in .m file
-(void)getSelection
{
//from date picker good part
NSLocale *usLocale = [[[NSLocale alloc]
initWithLocaleIdentifier:@"en_US"] autorelease];
NSDate *pickerDate = [datePicker date];
NSString *selectionString = [[NSString alloc] initWithFormat:@"%@",
[pickerDate descriptionWithLocale:usLocale]];
dateLabel.text = selectionString;
[selectionString release];
alarm = pickerDate;
//[mylabel setText:(@"text")]; //this string works
NSTimer *checkAlarm = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(checkAlarm:) userInfo:nil repeats:YES];
}
-(void)checkAlarm:(NSTimer *)t{
if ([NSDate date] == alarm){
// Alarm reached
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Alarm"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release] ;
[t invalidate];
}
}
but I it doesnt triggers alarm :( I cant figure out why.
check with breakpoint whether your alarm method is called ?? try like this ...something
-(void)getSelection
{
//from date picker good part
NSLocale *usLocale = [[[NSLocale alloc]
initWithLocaleIdentifier:@"en_US"] autorelease];
NSDate *pickerDate = [datePicker date];
NSString *selectionString = [[NSString alloc] initWithFormat:@"%@",
[pickerDate descriptionWithLocale:usLocale]];
dateLabel.text = selectionString;
[selectionString release];
alarm = pickerDate;
//[mylabel setText:(@"text")]; //this string works
NSTimer *checkAlarm =[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(checkAlarm:) userInfo:nil repeats:no];
}
-(void)checkAlarm{
if ([NSDate date] == alarm){
// Alarm reached
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Alarm"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
[alert release] ;
}
精彩评论