我正在尝试创建一个代码来测试UIDatePicker中选择的日期是否等于当前日期。我想把它作为一个闹钟(用户选择一个时间,他们按一个按钮,并在达到该时间时显示警报)。谢谢!这是我的代码:
#import "AlarmViewController.h"
@interface AlarmViewController ()
@end
@implementation AlarmViewController
NSDate *selectedDate;
NSDate *now;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
now = [NSDate date];
[_setDatePicker setDate:now animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)setAlarm:(id)sender {
selectedDate = [_setDatePicker date];
[self checkDate];
}
-(void)checkDate{
if(selectedDate == now){
NSString *message = [[NSString alloc]initWithFormat:@"The date and time you selected
is: %@", selectedDate];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"time selected" message:message
delegate:Nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
}
}
@end