//
// ViewController.m
// 手写UIDatePicker
//
// Created by 非凡 程序员 on 15/11/13.
// Copyright (c) 2015年 非凡 程序员. All rights reserved.
//
.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(nonatomic,strong)UIDatePicker *datePicker;
@end
---------------------------------
.m文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//定义一个datePicker
_datePicker = [ [ UIDatePicker alloc ] initWithFrame:CGRectMake (10, 10, 100, 100) ] ;
//设置datePicker的样式
_datePicker.datePickerMode = UIDatePickerModeDateAndTime ;
//设置datePicker的时间差
_datePicker.minuteInterval = 6;
//设置datePicker的最小时间为当前时间
_datePicker.minimumDate = [ NSDate date ] ;
//对datePicker添加事件
[ _datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged ] ;
//将控件添加在页面上
[ self.view addSubview:_datePicker ] ;
}
//当datePicker值发生改变会回调的自定义方法
- ( void ) dateChange : ( UIDatePicker * ) sender {
NSDate * date = _datePicker.date ; //获取datePicker的时间
NSLog ( @" %@ " , date )
;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
--------------------------------------------------------------------------------------------------------------
// ViewController.m
// UIDatePicker
//
// Created by 非凡 程序员 on 15/11/13.
// Copyright (c) 2015年 非凡 程序员. All rights reserved.
//
.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@end
-----------------------------------
.m文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// UIDatePicker 是一个控制器类,他是UIControl的子类,专门用于接受日期、时间和持续时长的输入。日期选取器的各列会按照指定的风格进行自动配置
// UIDatePicker 依赖于 NSDate 类
_datePicker.minuteInterval=6;
//设置datePicker的样式
_datePicker.datePickerMode=UIDatePickerModeDate;
// 格式化一个字符串型的时间为NSDate类型
NSString *string=@"2012-12-22";
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date=[formatter dateFromString:string];
// 设置datePicker的最小时间
_datePicker.minimumDate=date;
//对datePicker添加事件
[_datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
}
//当datePicker值发生改变会回调的自定义方法
-(void)dateChange:(UIDatePicker *)sender{
NSDate *date=_datePicker.date;
NSLog(@"%@",date);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end