码迷,mamicode.com
首页 > 其他好文 > 详细

UIDatePicker和UIPickerView实现生日选择、城市选择

时间:2016-04-29 15:48:15      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:

效果:

技术分享

1.控制器代码:

//
//  ViewController.m

#import "ViewController.h"
#import "Province.h"


@interface ViewController ()<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *birthdayField;
@property (weak, nonatomic) IBOutlet UITextField *cityField;

@property(nonatomic,weak)UIDatePicker *datePicker;
@property(nonatomic,strong)NSMutableArray *provinces;

@property(nonatomic,weak)UIPickerView *pickerView;
@property(nonatomic,assign)NSInteger provinceIndex;

@end

@implementation ViewController

- (NSMutableArray *)provinces
{
    if (_provinces == nil) {
        _provinces = [NSMutableArray array];
        
        // 字典转模型
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"provinces" ofType:@"plist"];
        NSArray *data = [NSArray arrayWithContentsOfFile:filePath];
        
        for (NSDictionary *dict in data) {
            Province *p = [Province provinceWithDict:dict];
            
            [_provinces addObject:p];
        }
    }
    return _provinces;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置文本框的代理
    _birthdayField.delegate = self;
    _cityField.delegate = self;
    
    // 自定义键盘
    [self setupBirthdayKeyboard];
    // 自定义城市键盘
    [self setupCityKeyboard];
}

- (void)setupCityKeyboard
{
    // 创建UIPickerView
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    pickerView.dataSource = self;
    pickerView.delegate = self;
    _pickerView = pickerView;
    
    //
    _cityField.inputView = pickerView;
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) { // 描述省
        return self.provinces.count;
    }else{ // 描述选中的省的城市
        Province *p = self.provinces[_provinceIndex];
        return p.cities.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) { // 描述省
        Province *p = self.provinces[row];
        return p.name;
    }else{ // 描述城市
        Province *p = self.provinces[_provinceIndex];
        return p.cities[row];
    }
}

#pragma mark - UIPickerViewDelegate
/**
 *  用户主动滚动pickerView就会调用
 */
 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) { // 滚动省份的时候,刷新城市
        // 记录当前选中的省份
        _provinceIndex = [pickerView selectedRowInComponent:0];
        
        // 刷新城市数据
        [pickerView reloadComponent:1];
    }
    
    
    // 给文本框赋值
    // 获取选中的省份
    Province *p = self.provinces[_provinceIndex];
    // 获取选中的城市
     NSInteger cityIndex = [pickerView selectedRowInComponent:1];
    NSString *cityName = p.cities[cityIndex];
    
    _cityField.text = [NSString stringWithFormat:@"%@ %@",p.name,cityName];
}

/**
 *  自定义生日键盘
 */
- (void)setupBirthdayKeyboard
{
    // 创建UIDatePicker
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    _datePicker = datePicker;
    
    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    datePicker.datePickerMode = UIDatePickerModeDate;
    
    // 添加监听
    [datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    
    //
    _birthdayField.inputView = datePicker;
}

/**
 *  当UIDatePicker滚动的时候调用
 */
- (void)dateChange:(UIDatePicker *)datePicker
{
//    NSLog(@"%@",datePicker.date);
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
    NSString *dateStr = [fmt stringFromDate:datePicker.date];
    
    // 给生日文本框赋值
    _birthdayField.text = dateStr;
}

#pragma mark - UITextFieldDelegate

/**
 *  是否允许开始编辑
 */
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
//{
//    return NO;
//}

/**
 *  文本框开始编辑的时候调用
 */
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == _birthdayField) { // 给生日文本框赋值
        
        [self dateChange:_datePicker];
    }else{ // 给城市文本框赋值
        [self pickerView:_pickerView didSelectRow:0 inComponent:0];
    }
    
}

/**
 *  是否允许用户输入文字
 */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return NO;
}

@end
2.Province模型
//
//  Province.h

#import <Foundation/Foundation.h>

@interface Province : NSObject

@property(nonatomic,strong)NSArray *cities;

@property(nonatomic,strong)NSString *name;

+ (instancetype)provinceWithDict:(NSDictionary *)dict;

@end
//
//  Province.m

#import "Province.h"

@implementation Province

+ (instancetype)provinceWithDict:(NSDictionary *)dict
{
    Province *p = [[self alloc] init];
    [p setValuesForKeysWithDictionary:dict];
    
    return p;
}

@end
3.省份城市数据plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<dict>
		<key>cities</key>
		<array>
			<string>福州</string>
			<string>龙岩</string>
			<string>南平</string>
			<string>宁德</string>
			<string>莆田</string>
			<string>泉州</string>
			<string>三明</string>
			<string>厦门</string>
			<string>永安</string>
			<string>漳州</string>
		</array>
		<key>name</key>
		<string>福建</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>济南</string>
			<string>滨州</string>
			<string>德州</string>
			<string>东营</string>
			<string>肥城</string>
			<string>海阳</string>
			<string>菏泽</string>
			<string>济宁</string>
			<string>莱芜</string>
			<string>莱阳</string>
			<string>聊城</string>
			<string>临沂</string>
			<string>平度</string>
			<string>青岛</string>
			<string>青州</string>
			<string>日照</string>
			<string>泰安</string>
			<string>威海</string>
			<string>潍坊</string>
			<string>烟台</string>
			<string>枣庄</string>
			<string>章丘</string>
			<string>淄博</string>
		</array>
		<key>name</key>
		<string>山东</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>成都</string>
			<string>阿坝</string>
			<string>巴中</string>
			<string>崇州</string>
			<string>达州</string>
			<string>大邑</string>
			<string>德阳</string>
			<string>都江堰</string>
			<string>峨眉山</string>
			<string>甘孜</string>
			<string>广安</string>
			<string>广元</string>
			<string>江油</string>
			<string>金堂</string>
			<string>乐山</string>
			<string>泸州</string>
			<string>眉山</string>
			<string>绵阳</string>
			<string>内江</string>
			<string>南充</string>
			<string>攀枝花</string>
			<string>遂宁</string>
			<string>西昌</string>
			<string>雅安</string>
			<string>宜宾</string>
			<string>资阳</string>
			<string>自贡</string>
		</array>
		<key>name</key>
		<string>四川</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>昆明</string>
			<string>保山</string>
			<string>楚雄</string>
			<string>大理</string>
			<string>德宏</string>
			<string>迪庆</string>
			<string>个旧</string>
			<string>丽江</string>
			<string>临沧</string>
			<string>怒江</string>
			<string>曲靖</string>
			<string>思茅</string>
			<string>文山</string>
			<string>西双版纳</string>
			<string>玉溪</string>
			<string>昭通</string>
		</array>
		<key>name</key>
		<string>云南</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>东城区</string>
			<string>西城区</string>
			<string>崇文区</string>
			<string>宣武区</string>
			<string>海淀区</string>
			<string>朝阳区</string>
			<string>丰台区</string>
			<string>石景山区</string>
			<string>通州区</string>
			<string>顺义区</string>
			<string>房山区</string>
			<string>大兴区</string>
			<string>昌平区</string>
			<string>怀柔区</string>
			<string>平谷区</string>
			<string>门头沟区</string>
			<string>密云县</string>
			<string>延庆县</string>
		</array>
		<key>name</key>
		<string>北京</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>石家庄</string>
			<string>保定</string>
			<string>泊头</string>
			<string>沧州</string>
			<string>承德</string>
			<string>邯郸</string>
			<string>河间</string>
			<string>衡水</string>
			<string>廊坊</string>
			<string>秦皇岛</string>
			<string>任丘</string>
			<string>唐山</string>
			<string>邢台</string>
			<string>张家口</string>
		</array>
		<key>name</key>
		<string>河北</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>南京</string>
			<string>常州</string>
			<string>高邮</string>
			<string>淮安</string>
			<string>连云港</string>
			<string>南通</string>
			<string>苏州</string>
			<string>宿迁</string>
			<string>太仓</string>
			<string>泰州</string>
			<string>无锡</string>
			<string>新沂</string>
			<string>徐州</string>
			<string>盐城</string>
			<string>扬州</string>
			<string>镇江</string>
		</array>
		<key>name</key>
		<string>江苏</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>台北</string>
			<string>高雄</string>
			<string>台南</string>
			<string>台中</string>
		</array>
		<key>name</key>
		<string>台湾</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>沈阳</string>
			<string>鞍山</string>
			<string>本溪</string>
			<string>朝阳</string>
			<string>大连</string>
			<string>丹东</string>
			<string>抚顺</string>
			<string>阜新</string>
			<string>葫芦岛</string>
			<string>锦州</string>
			<string>辽阳</string>
			<string>盘锦</string>
			<string>铁岭</string>
			<string>营口</string>
		</array>
		<key>name</key>
		<string>辽宁</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>长春</string>
			<string>白城</string>
			<string>白山</string>
			<string>珲春</string>
			<string>吉林</string>
			<string>辽源</string>
			<string>龙井</string>
			<string>舒兰</string>
			<string>四平</string>
			<string>松原</string>
			<string>通化</string>
			<string>延边</string>
		</array>
		<key>name</key>
		<string>吉林</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>呼和浩特</string>
			<string>阿拉善盟</string>
			<string>巴彦淖尔盟</string>
			<string>包头</string>
			<string>赤峰</string>
			<string>鄂尔多斯</string>
			<string>呼伦贝尔</string>
			<string>通辽</string>
			<string>乌海</string>
			<string>乌兰察布盟</string>
			<string>锡林郭勒盟</string>
			<string>兴安盟</string>
		</array>
		<key>name</key>
		<string>内蒙古</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>杭州</string>
			<string>北仑</string>
			<string>慈溪</string>
			<string>奉化</string>
			<string>湖州</string>
			<string>嘉兴</string>
			<string>金华</string>
			<string>丽水</string>
			<string>临海</string>
			<string>宁波</string>
			<string>宁海</string>
			<string>衢州</string>
			<string>三门</string>
			<string>绍兴</string>
			<string>台州</string>
			<string>天台</string>
			<string>温岭</string>
			<string>温州</string>
			<string>仙居</string>
			<string>象山</string>
			<string>义乌</string>
			<string>余姚</string>
			<string>舟山</string>
		</array>
		<key>name</key>
		<string>浙江</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>拉萨</string>
			<string>阿里</string>
			<string>昌都</string>
			<string>林芝</string>
			<string>那曲</string>
			<string>日喀则</string>
			<string>山南</string>
		</array>
		<key>name</key>
		<string>西藏</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>西宁</string>
			<string>果洛</string>
			<string>海北</string>
			<string>海东</string>
			<string>海南</string>
			<string>海西</string>
			<string>黄南</string>
			<string>玉树</string>
		</array>
		<key>name</key>
		<string>青海</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>澳门</string>
		</array>
		<key>name</key>
		<string>澳门</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>武汉</string>
			<string>鄂州</string>
			<string>恩施</string>
			<string>黄冈</string>
			<string>黄石</string>
			<string>荆门</string>
			<string>荆州</string>
			<string>潜江</string>
			<string>十堰</string>
			<string>随州</string>
			<string>天门</string>
			<string>仙桃</string>
			<string>咸宁</string>
			<string>襄樊</string>
			<string>孝感</string>
			<string>宜昌</string>
		</array>
		<key>name</key>
		<string>湖北</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>哈尔滨</string>
			<string>大庆</string>
			<string>大兴安岭</string>
			<string>鹤岗</string>
			<string>黑河</string>
			<string>虎林</string>
			<string>鸡西</string>
			<string>佳木斯</string>
			<string>密山</string>
			<string>牡丹江</string>
			<string>宁安</string>
			<string>七台河</string>
			<string>齐齐哈尔</string>
			<string>双鸭山</string>
			<string>绥化</string>
			<string>五常</string>
			<string>伊春</string>
		</array>
		<key>name</key>
		<string>黑龙江</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>南宁</string>
			<string>百色</string>
			<string>北海</string>
			<string>北流</string>
			<string>崇左</string>
			<string>防城港</string>
			<string>贵港</string>
			<string>桂林</string>
			<string>桂平</string>
			<string>河池</string>
			<string>贺州</string>
			<string>来宾</string>
			<string>柳州</string>
			<string>钦州</string>
			<string>梧州</string>
			<string>宜州</string>
			<string>玉林</string>
		</array>
		<key>name</key>
		<string>广西</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>郑州</string>
			<string>安阳</string>
			<string>鹤壁</string>
			<string>济源</string>
			<string>焦作</string>
			<string>开封</string>
			<string>洛阳</string>
			<string>漯河</string>
			<string>南阳</string>
			<string>平顶山</string>
			<string>濮阳</string>
			<string>三门峡</string>
			<string>商丘</string>
			<string>新乡</string>
			<string>信阳</string>
			<string>许昌</string>
			<string>周口</string>
			<string>驻马店</string>
		</array>
		<key>name</key>
		<string>河南</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>太原</string>
			<string>长治</string>
			<string>大同</string>
			<string>晋城</string>
			<string>晋中</string>
			<string>临汾</string>
			<string>吕梁</string>
			<string>朔州</string>
			<string>忻州</string>
			<string>阳泉</string>
			<string>运城</string>
		</array>
		<key>name</key>
		<string>山西</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>重庆</string>
		</array>
		<key>name</key>
		<string>重庆</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>南昌</string>
			<string>抚州</string>
			<string>赣州</string>
			<string>吉安</string>
			<string>景德镇</string>
			<string>九江</string>
			<string>萍乡</string>
			<string>上饶</string>
			<string>新余</string>
			<string>宜春</string>
			<string>鹰潭</string>
		</array>
		<key>name</key>
		<string>江西</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>天津</string>
		</array>
		<key>name</key>
		<string>天津</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>银川</string>
			<string>固原</string>
			<string>石嘴山</string>
			<string>吴忠</string>
			<string>中卫</string>
		</array>
		<key>name</key>
		<string>宁夏</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>西安</string>
			<string>安康</string>
			<string>宝鸡</string>
			<string>汉中</string>
			<string>商洛</string>
			<string>铜川</string>
			<string>渭南</string>
			<string>咸阳</string>
			<string>兴平</string>
			<string>延安</string>
			<string>榆林</string>
		</array>
		<key>name</key>
		<string>陕西</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>上海</string>
		</array>
		<key>name</key>
		<string>上海</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>香港</string>
		</array>
		<key>name</key>
		<string>香港</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>广州</string>
			<string>潮州</string>
			<string>从化</string>
			<string>东莞</string>
			<string>佛山</string>
			<string>河源</string>
			<string>鹤山</string>
			<string>化州</string>
			<string>惠州</string>
			<string>江门</string>
			<string>揭阳</string>
			<string>茂名</string>
			<string>梅州</string>
			<string>清远</string>
			<string>汕头</string>
			<string>汕尾</string>
			<string>韶关</string>
			<string>深圳</string>
			<string>阳江</string>
			<string>云浮</string>
			<string>湛江</string>
			<string>肇庆</string>
			<string>中山</string>
			<string>珠海</string>
		</array>
		<key>name</key>
		<string>广东</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>兰州</string>
			<string>白银</string>
			<string>定西</string>
			<string>甘南</string>
			<string>嘉峪关</string>
			<string>酒泉</string>
			<string>临夏</string>
			<string>陇南</string>
			<string>平凉</string>
			<string>庆阳</string>
			<string>天水</string>
			<string>武威</string>
			<string>张掖</string>
		</array>
		<key>name</key>
		<string>甘肃</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>贵阳</string>
			<string>安顺</string>
			<string>毕节</string>
			<string>都匀</string>
			<string>凯里</string>
			<string>六盘水</string>
			<string>铜仁</string>
			<string>兴义</string>
			<string>遵义</string>
		</array>
		<key>name</key>
		<string>贵州</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>乌鲁木齐</string>
			<string>阿克苏</string>
			<string>阿拉尔</string>
			<string>阿图什</string>
			<string>博乐</string>
			<string>昌吉</string>
			<string>哈密</string>
			<string>和田</string>
			<string>喀什</string>
			<string>克拉玛依</string>
			<string>库尔勒</string>
			<string>石河子</string>
			<string>图木舒克</string>
			<string>吐鲁番</string>
			<string>五家渠</string>
			<string>伊宁</string>
		</array>
		<key>name</key>
		<string>新疆</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>长沙</string>
			<string>常德</string>
			<string>郴州</string>
			<string>衡阳</string>
			<string>怀化</string>
			<string>吉首</string>
			<string>耒阳</string>
			<string>冷水江</string>
			<string>娄底</string>
			<string>韶山</string>
			<string>邵阳</string>
			<string>湘潭</string>
			<string>湘乡</string>
			<string>益阳</string>
			<string>永州</string>
			<string>岳阳</string>
			<string>张家界</string>
			<string>株州</string>
		</array>
		<key>name</key>
		<string>湖南</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>合肥</string>
			<string>安庆</string>
			<string>蚌埠</string>
			<string>亳州</string>
			<string>巢湖</string>
			<string>池州</string>
			<string>滁州</string>
			<string>阜阳</string>
			<string>淮北</string>
			<string>淮南</string>
			<string>黄山</string>
			<string>黄山景区</string>
			<string>九华山景区</string>
			<string>六安</string>
			<string>马鞍山</string>
			<string>青阳</string>
			<string>宿州</string>
			<string>铜陵</string>
			<string>芜湖</string>
			<string>宣城</string>
		</array>
		<key>name</key>
		<string>安徽</string>
	</dict>
	<dict>
		<key>cities</key>
		<array>
			<string>海口</string>
			<string>儋州</string>
			<string>东方</string>
			<string>琼海</string>
			<string>三亚</string>
			<string>万宁</string>
			<string>文昌</string>
			<string>五指山</string>
		</array>
		<key>name</key>
		<string>海南</string>
	</dict>
</array>
</plist>





UIDatePicker和UIPickerView实现生日选择、城市选择

标签:

原文地址:http://blog.csdn.net/github_26672553/article/details/51277998

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!