日期选择代码,使用datepicker
2013-03-06 21:22
447 查看
日期选择代码,使用datepicker
- (void)viewDidLoad
{
[super viewDidLoad];
//日期
lessonDateTextField_ = [ [ UITextField alloc ] init ];
lessonDateTextField_.frame = CGRectMake( newPoint.x , newPoint.y , dudaoTextFieldWidth , dudaoTextFieldHeight );
lessonDateTextField_.borderStyle = UITextBorderStyleRoundedRect;
lessonDateTextField_.font =[ UIFont fontWithName: @"Noteworthy-Light" size: 18 ];
//文本输入框添加按钮
UIButton* lessonDateButton = [ UIButton buttonWithType: UIButtonTypeDetailDisclosure ];
[ lessonDateButton setImage: buttonImage forState: UIControlStateNormal ];
[ lessonDateButton addTarget: self
action: @selector(lessonDateButtonDidPush)
forControlEvents: UIControlEventTouchUpInside ];
lessonDateTextField_.rightViewMode = UITextFieldViewModeAlways;
lessonDateTextField_.rightView = lessonDateButton;
lessonDateTextField_.delegate = self;
[ self.view addSubview: lessonDateTextField_ ];
//*------------------------------------初始化时间选择器--------------------------------------*//
//日期,显示timepicker,界面整体上移
datePicker_ = [ [ UIDatePicker alloc ] init ];
datePicker_.frame = CGRectMake( 0 , 480 , 320 , 216 );
datePicker_.datePickerMode = UIDatePickerModeDate;
[ self.view addSubview: datePicker_ ];
//*----------------------------------------------------------------------------------------*//
}
//日期选择按钮
- (void)lessonDateButtonDidPush
{
//tableview都移开
[ classTypeTableView_ removeFromSuperview ];
[ lessonClassroomTableView_ removeFromSuperview ];
[ lessonSectionTableView_ removeFromSuperview ];
[ schoolDistrictTableView_ removeFromSuperview ];
//设置完成按钮
UIBarButtonItem* doneButton =
[ [ [ UIBarButtonItem alloc ]
initWithBarButtonSystemItem: UIBarButtonSystemItemDone
target:self
action: @selector(doneDidPush) ] autorelease ];
[ self setToolbarItems: [ NSArray arrayWithObject: doneButton ] animated: YES ];
[ UIView beginAnimations: nil context: nil ];
[ UIView setAnimationDuration: 0.3 ];
//时间选择器上移
datePicker_.frame = CGRectMake( 0 , 216 , 320 , 216 );
//工具条上移
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y = self.view.frame.size.height - toolbarFrame.size.height - 50 ;
self.navigationController.toolbar.frame = toolbarFrame;
[ UIView commitAnimations ];
}
//日期选择完成按钮
- (void)doneDidPush
{
NSDate* date = [ datePicker_ date ];
NSDateFormatter* dateFormatter = [ [ NSDateFormatter alloc ] init ];
[ dateFormatter setDateFormat: @"yyyy-MM-dd" ];
lessonDateTextField_.text = [ dateFormatter stringFromDate: date ];
[ UIView beginAnimations: nil context: nil ];
[ UIView setAnimationDuration: 0.3 ];
//隐藏工具条
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y =
self.view.bounds.size.height + 65 ;
self.navigationController.toolbar.frame = toolbarFrame;
//隐藏完成按钮
UIBarButtonItem* spaceButton = [ [ [ UIBarButtonItem alloc ]
initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace
target: nil action: nil ] autorelease ];
[ self setToolbarItems: [ NSArray arrayWithObject: spaceButton ] animated: YES ];
//隐藏日期选择器
datePicker_.frame = toolbarFrame;
[ UIView commitAnimations ];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//日期
lessonDateTextField_ = [ [ UITextField alloc ] init ];
lessonDateTextField_.frame = CGRectMake( newPoint.x , newPoint.y , dudaoTextFieldWidth , dudaoTextFieldHeight );
lessonDateTextField_.borderStyle = UITextBorderStyleRoundedRect;
lessonDateTextField_.font =[ UIFont fontWithName: @"Noteworthy-Light" size: 18 ];
//文本输入框添加按钮
UIButton* lessonDateButton = [ UIButton buttonWithType: UIButtonTypeDetailDisclosure ];
[ lessonDateButton setImage: buttonImage forState: UIControlStateNormal ];
[ lessonDateButton addTarget: self
action: @selector(lessonDateButtonDidPush)
forControlEvents: UIControlEventTouchUpInside ];
lessonDateTextField_.rightViewMode = UITextFieldViewModeAlways;
lessonDateTextField_.rightView = lessonDateButton;
lessonDateTextField_.delegate = self;
[ self.view addSubview: lessonDateTextField_ ];
//*------------------------------------初始化时间选择器--------------------------------------*//
//日期,显示timepicker,界面整体上移
datePicker_ = [ [ UIDatePicker alloc ] init ];
datePicker_.frame = CGRectMake( 0 , 480 , 320 , 216 );
datePicker_.datePickerMode = UIDatePickerModeDate;
[ self.view addSubview: datePicker_ ];
//*----------------------------------------------------------------------------------------*//
}
//日期选择按钮
- (void)lessonDateButtonDidPush
{
//tableview都移开
[ classTypeTableView_ removeFromSuperview ];
[ lessonClassroomTableView_ removeFromSuperview ];
[ lessonSectionTableView_ removeFromSuperview ];
[ schoolDistrictTableView_ removeFromSuperview ];
//设置完成按钮
UIBarButtonItem* doneButton =
[ [ [ UIBarButtonItem alloc ]
initWithBarButtonSystemItem: UIBarButtonSystemItemDone
target:self
action: @selector(doneDidPush) ] autorelease ];
[ self setToolbarItems: [ NSArray arrayWithObject: doneButton ] animated: YES ];
[ UIView beginAnimations: nil context: nil ];
[ UIView setAnimationDuration: 0.3 ];
//时间选择器上移
datePicker_.frame = CGRectMake( 0 , 216 , 320 , 216 );
//工具条上移
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y = self.view.frame.size.height - toolbarFrame.size.height - 50 ;
self.navigationController.toolbar.frame = toolbarFrame;
[ UIView commitAnimations ];
}
//日期选择完成按钮
- (void)doneDidPush
{
NSDate* date = [ datePicker_ date ];
NSDateFormatter* dateFormatter = [ [ NSDateFormatter alloc ] init ];
[ dateFormatter setDateFormat: @"yyyy-MM-dd" ];
lessonDateTextField_.text = [ dateFormatter stringFromDate: date ];
[ UIView beginAnimations: nil context: nil ];
[ UIView setAnimationDuration: 0.3 ];
//隐藏工具条
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y =
self.view.bounds.size.height + 65 ;
self.navigationController.toolbar.frame = toolbarFrame;
//隐藏完成按钮
UIBarButtonItem* spaceButton = [ [ [ UIBarButtonItem alloc ]
initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace
target: nil action: nil ] autorelease ];
[ self setToolbarItems: [ NSArray arrayWithObject: spaceButton ] animated: YES ];
//隐藏日期选择器
datePicker_.frame = toolbarFrame;
[ UIView commitAnimations ];
}
相关文章推荐
- 日期滚动选择(使用DatePicker实现)
- android—DatePicker 和TimePicker显示日期以及使用TimePickerDialog,DatePickerDialog来专门实现时间选择对话框
- Android-使用TimerPicker和DatePicker选择时间和日期
- Flask学习笔记-使用bootstrap-datepicker实现页面日期选择
- 代码实现日期选择器UIDatePicker使用
- 使用WdatePicker日期组件时,选择日期后,执行某个方法
- Date Range Picker和bootstrapValidator同时使用时,选择日期后无法正常触发校验
- Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能
- javascript 日期联动选择器 [其中的一些代码值得学习]
- JQuery UI之(五)日期选择——Datepicker
- 使用bootstrap-datepicker的beforeShowDay给日历添加特殊日期及样式
- 安装AIC准则使用前进法后退法和逐步回归法进行变量选择的r语言代码
- 还是mui的日期选择插件使用,可以在弹出日期选择之前提问他是否想修改
- 日期选择器:jquery datepicker的使用
- python使用选择结构鉴别合法日期
- 使用WdatePicker.js显示日期选择控件,并进行设置
- 发布一个的Web日期选择控件及源码(其中的js代码来自于Microsoft CRM)
- 在SQL中使用convert函数进行日期的查询的代码
- 从零开始学android<日期选择器:DatePicker的使用.十二.>
- guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁