日期选择滚轮

//
//  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值