手写UIDatePicker和UIPickerView

//

//  ViewController.h

//  手写UIDatePicker

//

//  Created by 非凡 程序员 on 15/11/13.

//  Copyright (c) 2015 非凡 程序员. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface  ViewController : UIViewController


@property(nonatomic,strong)UIDatePicker *datePicker;

@end



//

//  ViewController.m

//  手写UIDatePicker

//

//  Created by 非凡 程序员 on 15/11/13.

//  Copyright (c) 2015 非凡 程序员. All rights reserved.

//


#import "ViewController.h"


@interface  ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

  //定义一个datePicker

    _datePicker = [ [ UIDatePicker alloc ] initWithFrame:CGRectMake (1010100100) ] ;

    //设置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.h

//  手写UIPickerView

//

//  Created by 非凡 程序员 on 15/11/13.

//  Copyright (c) 2015 非凡 程序员. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>



@end



//

//  ViewController.m

//  手写UIPickerView

//

//  Created by 非凡 程序员 on 15/11/13.

//  Copyright (c) 2015 非凡 程序员. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //添加一个pickerView的步骤:

//    1.实例化一个pickerView对象

    UIPickerView *picker=[[UIPickerView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

//    2,遵守两个协议,并设置代理

    picker.dataSource=self;

    picker.delegate=self;

//    3.将控件添加在页面上

    [self.view addSubview:picker];

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

//实现两个必要方法。设置行数和组数

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return 4;

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 7;

}


@end


你可能感兴趣的:(手写UIDatePicker和UIPickerView)