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