//
//  ViewController.m
//  04-键盘处理
//

//

#import "ViewController.h"
#import "XMGProvince.h"

@interface ViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *birthdayField;

@property (nonatomic, weak) UIDatePicker *datePicker;

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

@property (nonatomic, strong) NSMutableArray *provinces;

@property (nonatomic, assign) NSInteger proIndex;

@end

@implementation ViewController

// 懒加载省会
- (NSMutableArray *)provinces
{
    if (_provinces == nil) {
        // 装所有的省会
        _provinces = [NSMutableArray array];
        
        // 加载plist文件
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil];
        NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
        
        for (NSDictionary *dict in arr) {
            // 字典转模型
            XMGProvince *p = [XMGProvince provinceWithDict:dict];
            
            [_provinces addObject:p];
        }
    }
    return _provinces;
}

#pragma mark - UITextFieldDelegate

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

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

#pragma mark -viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 设置文本框的代码
    _birthdayField.delegate = self;
    _cityField.delegate = self;
    
    // 自定义生日键盘
    [self setUpBirthdayKeyboard];
    
    // 自定义城市键盘
    [self setUpCityKeyboard];
    
}
#pragma mark - 自定义城市键盘
- (void)setUpCityKeyboard
{
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    
    _pickerView = pickerView;
    
    pickerView.dataSource = self;
    pickerView.delegate = self;
    
    _cityField.inputView = pickerView;
}

#pragma mark -UIPickerView
#pragma mark UIPickerView的数据源
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

// pickerView的第0列描述省会,有多少个省
// pickerView的第1列描述选中的省会,有多少个城市
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) { // 描述省会
        
        return self.provinces.count;
        
        
    }else{ // 描述选中的省会的城市
        
     
        
        // 获取省会
        XMGProvince *p = self.provinces[_proIndex];
        
        return p.cities.count;
        
    }
}
#pragma mark -UIPickerView的代理
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) { // 描述省会
        
        // 获取省会
        XMGProvince *p = self.provinces[row];
        return p.name;
        
    }else{ // 描述选中的省会的城市
        // 获取选中的省会的角标
        NSInteger index = [pickerView selectedRowInComponent:0];
        
        // 获取选中省会
        XMGProvince *p = self.provinces[_proIndex];

// 当前选中的内蒙古省,只有12个城市,角标0~11,但是右边城市是北京,北京的城市大于12个城市,所以滚动的时候会出现越界。
        
        NSLog(@"province:%@, count:%ld row:%ld",p.name,p.cities.count,row);
        
#warning TODO:
        return p.cities[row];
    }
}

// 全局断点就是帮我们定位到出bug的那一行。

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

#pragma mark -自定义生日键盘
- (void)setUpBirthdayKeyboard
{
    // 创建UIDatePicker
    // 注意:UIDatePicker有默认的尺寸,可以不用设置frame
    UIDatePicker *picker = [[UIDatePicker alloc] init];
    
    _datePicker = picker;
    
    // 设置地区 zh:中国
    picker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    
    // 设置日期的模式
    picker.datePickerMode = UIDatePickerModeDate;
    
    // 监听UIDatePicker的滚动
    [picker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    
    
    _birthdayField.inputView = picker;
}

// 当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;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

UIPickView之自定义生日键盘和城市键盘的更多相关文章

  1. JAVA之旅(二十七)——字节流的缓冲区,拷贝mp3,自定义字节流缓冲区,读取键盘录入,转换流InputStreamReader,写入转换流,流操作的规律

    JAVA之旅(二十七)--字节流的缓冲区,拷贝mp3,自定义字节流缓冲区,读取键盘录入,转换流InputStreamReader,写入转换流,流操作的规律 我们继续来聊聊I/O 一.字节流的缓冲区 这 ...

  2. 从MyEclipse到IntelliJ IDEA ——让你脱键盘,全键盘操作

    从MyEclipse到IntelliJ IDEA ——让你脱键盘,全键盘操作 从MyEclipse转战到IntelliJ IDEA的经历 我一个朋友写了一篇“从Eclipse到Android Stud ...

  3. h5仿微信、支付宝数字键盘|微信支付键盘|支付宝付款键盘

    html5仿微信支付数字键盘|仿支付宝键盘|h5仿微信密码输入键盘|自定义数字键盘 很早之前由于项目需求,就有开发过一个h5仿微信支付键盘,这几天就把之前的数字键盘模块独立出来,重新整理开发成demo ...

  4. GetKeyboardType获取键盘类型(通过键盘可初步判断用户使用的是台式电脑还是笔记本电脑)

    函数功能:该函数获取系统当前键盘的信息. int WINAPI GetKeyboardType( __in int nTypeFlag ); 参数说明:nTypeFlag:指定要获取的键盘信息的类型, ...

  5. 十天学会单片机Day2键盘检测(独立键盘、矩阵键盘)

    1.键盘的分类 编码键盘:键盘上闭合键的识别由专用的硬件编码器实现,并产生键编码号或键值的称为编码键盘,如计算机键盘 非编码键盘:靠软件编程来识别的称为非编码键盘.独立键盘.矩阵键盘 2.按键消抖   ...

  6. 隐藏虚拟键盘,解决键盘挡住UITextField问题

    再正式开始之前,先来介绍一下IOS的键盘类型: 一.键盘风格 UIKit框架支持8种风格键盘 ? 1 2 3 4 5 6 7 8 9 10 typedef enum {      UIKeyboard ...

  7. Xcode6.1模拟器ios8.1模拟器不能弹出虚拟键盘及虚拟键盘无法切换中文输入的解决办法

    1.不能弹出虚拟键盘的解决办法 模拟器菜单Hardware->Keyboard->Connect Hardware Keyboard取消选中,快捷键commad+shift+K 2.虚拟键 ...

  8. ios7学习之路七(隐藏虚拟键盘,解决键盘挡住UITextField问题)

    再正式开始之前,先来介绍一下IOS的键盘类型: 一.键盘风格 UIKit框架支持8种风格键盘 typedef enum { UIKeyboardTypeDefault, // 默认键盘:支持所有字符 ...

  9. UI弹出键盘和收回键盘

    点击textfield,会自动弹出键盘 要让键盘收回来,先设置个代理:[field setTextFieldDelegate:self];  可设置成自己,也可设置成其他对象,只要在对应的类中,遵循U ...

随机推荐

  1. 对“xxx”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。

    在程序中调用C++链接库中的回调函由于没有考虑生命周期,直接写委托回随机的被gc给回收掉导致报这个错误 错误的程序: private void InitPlateIdentify() { try { ...

  2. AOP 切面编程

    简介 如果你很熟悉面向方面编程(AOP),你就会知道给代码增加“切面”可以使代码更清晰并且具有可维护性.但是AOP通常都依赖于第三方类库或者硬编码的.net特性来工作.虽然这些实现方式的好处大于它们的 ...

  3. Listview 多个ViewHolder实现

    简单代码示例: package com.edaixi.adapter; import android.content.Context; import android.view.View; import ...

  4. css3图片3D翻转效果

    点击图片看翻转效果 html结构 <div class="flip"> <div class="front"> <img src= ...

  5. 射频识别技术漫谈(29)——射频接口芯片TRF7960

    TRF7960系列是TI推出的载波为13.56MHz.支持ISO15693.ISO14443A/B和FeliCa协议的射频接口芯片.许多人更关心它能不能读写MF1卡片,就我的理解及实际验证,由于MF1 ...

  6. DateTimePicker——开源的Android日历类库

    Github托管地址:https://github.com/flavienlaurent/datetimepicker

  7. 【玩转Ubuntu】08. Linux报错:Syntax error: "(" unexpected解决办法

    问题: 在MAC上写了一段shell脚本,放到Ubuntu上运行总是报下面这个错误,单步调试都是对的,就是直接运行会报错. bixiaopeng@ubuntu:~/package$ sh packag ...

  8. rsyslog 日志归类思路--根据syslog local5 nginx-zjzc01;

    Aug 5 16:36:12 jrhwpt01 nginx-zjzc01: www.zjcap.cn 10.252.105.157 10.171.246.184 [05/Aug/2016:16:36: ...

  9. cocos2dx 在mac下开发ios和android游戏

    这里主要说android,因为ios开发在文章 http://blog.csdn.net/itcastcpp/article/details/24792323 中已经说过. 1)打开工程 打开ecli ...

  10. delphi ICS控件示例解读

    {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Author: Fran鏾is PIETTE ...