选定时间的 iPhone 闹钟

发布于 2024-11-07 16:12:52 字数 1463 浏览 3 评论 0原文

我在创建闹钟应用程序时遇到问题。 我拥有的只是日期选择器、标签、按钮。

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

,但它不会触发警报:( 我不明白为什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小霸王臭丫头 2024-11-14 16:12:52

用断点检查你的警报方法是否被调用?
尝试这样...一些东西

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

    }

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

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文