我想做一个定制闹钟。所以我用了Datepicker。现在我想通过Datepicker在选定的时间发生时播放声音。
所以谁能告诉我哪些类或API的使用或任何方向作出?我应该如何使我所选择的时间与系统时间匹配,以创建一个事件。我知道怎么演奏。休息对我来说是个大问号。如有任何帮助,我将不胜感激。
直到现在我已经写了这段代码,这只是显示一个日期picker和点击按钮,它显示所选的时间。: P
代码:
**.h File**
#import <UIKit/UIKit.h>
@interface The420DudeViewController : UIViewController {
IBOutlet UINavigationBar *titleBar;
IBOutlet UIButton *setAlarmButton;
IBOutlet UIDatePicker *selectTimePicker;
}
@property(nonatomic, retain) IBOutlet UINavigationBar *titleBar;
@property(nonatomic, retain) IBOutlet UIButton *setAlarmButton;
@property(nonatomic, retain) IBOutlet UIDatePicker *selectTimePicker;
-(IBAction)onTapSetAlarm;
@end
**.m file **
@implementation The420DudeViewController
@synthesize titleBar,setAlarmButton,selectTimePicker;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
-(IBAction)onTapSetAlarm
{
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm a"];
NSDate *selectedDate = [[NSDate alloc] init];
selectedDate = [selectTimePicker date];
NSString *theTime = [timeFormat stringFromDate:selectedDate];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Time selected" message:theTime delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
// [timeFormat release];
// [selectedDate release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[titleBar release];
[setAlarmButton release];
[selectTimePicker release];
}
@end
您应该查看UILocalNotification
类-这将允许您的闹钟响起,即使在应用程序关闭/在后台:https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/UILocalNotification_Class.pdf
只需将localNotification的fireDate
设置为[selectTimePicker date]
,然后进行调度。