[非凡程序員]手寫UIDatePicker和UIPickerView

//程序員

//  ViewController.hatom

//  手寫UIDatePickerspa

//.net

//  Created by 非凡 程序員 on 15/11/13.3d

//  Copyright (c) 2015 非凡 程序員. All rights reserved.代理

//component


#import <UIKit/UIKit.h>orm


@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 (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.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

相關文章
相關標籤/搜索