//
// SelectTimeViewController.m
// EmploymentApp
//
// Created by Jester on 15-9-9.
// Copyright (c) 2015年 sss. All rights reserved.
//
#import "SelectTimeViewController.h"
@interface SelectTimeViewController (){
NSString *selectdate;
NSString *selectTime;
UIView *view;
}
@property (nonatomic, strong)UIDatePicker *datePickerView;
@property (nonatomic, strong)UIDatePicker *timePickerView;
@end
@implementation SelectTimeViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createGobackButton];
[self setViewControllTitle:@"请选择时间"];
[self createDatePickerView];
[self createTimePickerView];
[self setRightBarButtonItemTitle:@"确认" Selector:@selector(confirmButtonClick) delegate:self];
}
- (void)createTimePickerView{
//时分
self.timePickerView = [[ UIDatePicker alloc] init];
self.timePickerView.layer.cornerRadius = 6;
self.timePickerView.clipsToBounds = YES;
self.timePickerView.backgroundColor=[UIColor colorWithHexString:@"#f2f2f2"];
self.timePickerView.frame = CGRectMake(20, self.datePickerView.bottom+20, screenWidth-40, 105.5);
self.timePickerView.datePickerMode = UIDatePickerModeTime;
NSDateFormatter* formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"HH:mm"];
NSDate *defaultDate= [NSDate date];
self.datePickerView.date = defaultDate;
selectTime=[formater stringFromDate:defaultDate];
[self.timePickerView addTarget:self action:@selector(timeChanged:) forControlEvents:UIControlEventValueChanged ];
[view addSubview:self.timePickerView];
view.height = self.timePickerView.bottom+20;
}
- (void)timeChanged:(id)sender{
UIDatePicker *control = (UIDatePicker*)sender;
NSDate *_date = control.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
selectTime = [formatter stringFromDate:_date];
}
-(void)createDatePickerView
{
view = [[UIView alloc]initWithFrame:CGRectMake(0, 10, screenWidth, 261)];
view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:view];
//年月日
self.datePickerView = [[ UIDatePicker alloc] init];
self.datePickerView.layer.cornerRadius = 6;
self.datePickerView.clipsToBounds = YES;
self.datePickerView.backgroundColor=[UIColor colorWithHexString:@"#f2f2f2"];
self.datePickerView.frame = CGRectMake(20, 15, screenWidth-40, 105.5);
self.datePickerView.datePickerMode = UIDatePickerModeDate;
NSDateFormatter* formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd"];
NSDate *mindate = [NSDate date];
NSDate* maxdate = [formater dateFromString:@"2300-01-01"];
NSDate *defaultDate = [NSDate date];
self.datePickerView.maximumDate = maxdate;
self.datePickerView.minimumDate = mindate;
self.datePickerView.date = defaultDate;
selectdate=[formater stringFromDate:defaultDate];
[self.datePickerView addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];
[view addSubview:self.datePickerView];
}
//选中日期波轮的时间
-(void)dateChanged:(id)sender{
UIDatePicker *control = (UIDatePicker*)sender;
NSDate *_date = control.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
selectdate = [formatter stringFromDate:_date];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark---------confirmButtonClick----------
- (void)confirmButtonClick{
NSString *time = [NSString stringWithFormat:@"%@ %@",selectdate,selectTime];
[[NSNotificationCenter defaultCenter]postNotificationName:@"postTime" object:time];
[self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
转载于:https://my.oschina.net/wenchengxu123/blog/492269