iOS开发UIDatePicker,UIPickerView

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
{
    // 省份选择器数据源
    NSArray *_province;
    // 城市选择器数据源
    NSDictionary *_cities;
}
@property (strong, nonatomic) IBOutlet UITextField *cityTextField;
@property (strong, nonatomic) IBOutlet UITextField *dateTextField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载数据
    [self initWithData];
    [self initWithCityPickView];
    [self initWithDatePicker];
}

- (void)initWithDatePicker
{
    // 初始化日期选择器控件
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    //设置选择模式:日期、时间、日期&时间
    datePicker.datePickerMode = UIDatePickerModeDate;
    //设置日期的区域
    [datePicker setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_cn"]];
    //增加监听方法
    [datePicker addTarget:self action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];
    // 设置日期的输入键盘视图
    _dateTextField.inputView = datePicker;
}

- (void)datePickerChanged:(UIDatePicker *)sender
{
    // 需要将日期选择的结果转换成字符串
    // 1. 实例化日期格式器
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    // 2. 设置日期格式器的“格式字符串”
    [formatter setDateFormat:@"yyyy-MM-dd"];
    // 3. 将日期转换成文本
    NSString *dateString = [formatter stringFromDate:sender.date];
    // 4. 设置生日的文字
    _dateTextField.text = dateString;
}

- (void)initWithCityPickView
{
    UIPickerView *pickView = [[UIPickerView alloc] init];
    pickView.tag = 1;
    pickView.delegate = self;
    pickView.dataSource = self;
    pickView.showsSelectionIndicator = YES;
    _cityTextField.inputView = pickView;
}

//数据源
- (void)initWithData
{
    //城市
    NSString *path = [[NSBundle mainBundle]pathForResource:@"cities" ofType:@"plist"];
    NSDictionary *cities = [NSDictionary dictionaryWithContentsOfFile:path];
    _province = [NSArray array];
    _cities = [NSDictionary dictionary];
    _province = cities[@"provinces"];
    _cities = cities[@"cities"];

}

#pragma mark 设置列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

#pragma mark 设置行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
        if (component == 0) {
            return _province.count;
        } else {
            NSInteger row = [pickerView selectedRowInComponent:0];
            NSString *proinceName = _province[row];
            NSArray *city = _cities[proinceName];
            return city.count;
        }
}

#pragma mark 设置选择器行的内容的
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
        if (component == 0) {
            return _province[row];
        } else {
            NSInteger rowProvince = [pickerView selectedRowInComponent:0];
            NSString *provinceName = _province[rowProvince];
            NSArray *citys = _cities[provinceName];
            return citys[row];
        }
}

#pragma mark 选中行的时候,刷新数据
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //刷新数据
    [pickerView reloadComponent:1];

    //显示数据
    NSInteger row1 = [pickerView selectedRowInComponent:0];
    NSInteger row2 = [pickerView selectedRowInComponent:1];
    NSString *proviceName = _province[row1];
    NSArray *citys = _cities[proviceName];
    NSString *str = [NSString stringWithFormat:@"%@ %@", proviceName, citys[row2]];
    _cityTextField.text = str;

}
@end

你可能感兴趣的:(ios,选择器,Datepicker,城市选择,DatePicke)