在选定的时间iphone报警

时间:2011-05-16 12:44:25

标签: iphone timer alarm

我无法创建闹钟应用。 我只有Datepicker,标签,按钮。

@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

和.m文件

    -(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]; 
    }
}

但我没有触发警报:(我无法弄清楚原因。

1 个答案:

答案 0 :(得分:0)

用断点检查你的报警方法是否被调用? 尝试这样......某事

-(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]  ;   

    }