IOS_UI_Picker

来源:互联网 发布:政府网络舆情管理 编辑:程序博客网 时间:2024/06/18 09:29
H:/0828/01.DatePicker_ViewController.h
////  ViewController.h//  PickerView-01.DatePicker演练////  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end

H:/0828/01.DatePicker_ViewController.m
//  ViewController.m//  PickerView-01.DatePicker演练//  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.#import "ViewController.h"@interface ViewController ()// 生日文本属性@property (strong, nonatomic) UITextField *birthdayText;@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    // 1. 创建界面UI    // 1.1 Label    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20.0, 20.0, 80.0, 40)];    [label setText:@"生日"];    [self.view addSubview:label];    // 1.2 TextField    UITextField *birthdayText = [[UITextField alloc]initWithFrame:CGRectMake(100.0, 20.0, 200.0, 40)];//设置占位符:setPlaceholder    [birthdayText setPlaceholder:@"请输入生日"];    [birthdayText setBorderStyle:UITextBorderStyleRoundedRect];    _birthdayText = birthdayText;    [self.view addSubview:birthdayText];    // 1.3 日期选择控件    UIDatePicker *datePicker = [[UIDatePicker alloc]init];//    [self.view addSubview:datePicker];    // 1) 模式有3种:日期、时间、日期和时间    [datePicker setDatePickerMode:UIDatePickerModeDate];        // 打印所有可用的地区    NSLog(@"%@", [NSLocale availableLocaleIdentifiers]);    // 2) 设置日期选择控件的地区    [datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];    // 3) 监听日期选择控件数值变化    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];// 4)设置日期控件的初始值    // 4.1 指定一个字符串    NSString *dateString = @"1990-06-07";    // 4.2 把字符串转换成日期    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];    // A) 指定日期格式    [formatter setDateFormat:@"yyyy-MM-dd"];    // B) 字符串转成日期    NSDate *date = [formatter dateFromString:dateString];    // 4.3 将转换后的日期设置给日期选择控件,即设置默认的日期值    [datePicker setDate:date];    // 自定义生日文本输入要使用的视图,可以使用自定义视图充当文本的输入视图    [birthdayText setInputView:datePicker];}#pragma mark - 日期选择控件的监听方法- (void)dateChanged:(UIDatePicker *)datePicker{    NSLog(@"%@", datePicker.date);    // 通过查看NSDate.h文件,发现没有直接的办法转换日期格式    NSDate *date = datePicker.date;    // 1. 要转换日期格式,需要使用NSDateFormatter,专门用来转换日期格式的对象    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];    // 1.1 先设置日期的格式字符串     [formatter setDateFormat:@"yyyy-MM-dd"];    // 1.2 日期转成字符串~使用格式字符串,将日期转换成字符串    NSString *dateString = [formatter stringFromDate:date];    // 设置文本    [_birthdayText setText:dateString];//    [_birthdayText setText:datePicker.date.description];}@end

H:/0828/02.照片选择_ViewController.h
////  ViewController.h//  PickerView-02.照片选择////  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>// 头像视图@property (weak, nonatomic) IBOutlet UIImageView *imageView;// 选择照片操作- (IBAction)selectPhoto;@end

H:/0828/02.照片选择_ViewController.m
//  ViewController.m//  PickerView-02.照片选择//  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.#import "ViewController.h"@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>//成员变量 _imageView@end@implementation ViewController- (void)viewDidLoad{    // 一定要记住要实现父类的方法!!!    [super viewDidLoad];      // ... 在这里实例化各个控件,文本、图像、按钮}#pragma mark 照片选择器代理方法//照片选择完成,点击图片时,调用代理的该方法,参数中含有选取的图像// 注意:实现了这一代理方法之后,需要手动关闭照片选择控制器//默认选取完图片后,UIImagePickerController不会自己关闭消失- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{    // 目的:    NSLog(@"%@", info);       // 1. 当setAllowsEditing时,可取出编辑后的图像(原始图像是OriginalImage)    UIImage *image = info[@"UIImagePickerControllerEditedImage"];    // 2. 设置图像    [_imageView setImage:image];    // 让当前控制器关闭照片选择控制器    [self dismissViewControllerAnimated:YES completion:nil];}#pragma mark 选择头像视图// 响应按钮点击,选择照片操作- (IBAction)selectPhoto{    // 要选择头像,需要使用UIImagePickerController    // 1. 实例化照片选择控制器    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];    // 2. 设置照片源    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];    // 3. 设置是否允许编辑    [imagePicker setAllowsEditing:YES];        // 4. 设置控制器为代理,前提代理必须遵守2个协议//<UINavigationControllerDelegate, UIImagePickerControllerDelegate>    [imagePicker setDelegate:self];        // 5. 展现图片选择控制器,由当前视图控制器负责展现照片选择控制器    [self presentViewController:imagePicker animated:YES completion:nil];}@end

H:/0828/03.PickerView_ViewController.h
////  ViewController.h//  PickerView-03.PickerView////  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>@property (weak, nonatomic) IBOutlet UIPickerView *picker;@end

H:/0828/03.PickerView_ViewController.m
//  ViewController.m//  PickerView-03.PickerView//  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.#import "ViewController.h"@interface ViewController (){    // 第一列的数据源    NSArray *_colOne;    // 第二列的数据源    NSArray *_colTwo;}@end@implementation ViewController/* 在指定PickerView的数据源之前,它是不会显示的 一旦通过PickView的数据源方法指定了数据源,PickerView就可以显示了*/- (void)viewDidLoad{    [super viewDidLoad];    // 定义选择器的数据源    _colOne = @[@"a", @"b", @"c"];    _colTwo = @[@"1", @"2", @"3", @"4", @"5"];}#pragma mark - PickerView 数据源方法// 选择器中的列数(Component)- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 2;}// 每一列(Component)的行数(row)- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    if (component == 0) {        return _colOne.count;    } else {        return _colTwo.count;    }}#pragma mark - PickerView 代理方法// 设置component列row行显示的字符串内容- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{    if (component == 0) {        return _colOne[row];    } else {        return _colTwo[row];    }}// 选中第component列row行的内容// 参数说明:// component:当前选中的列// row:当前选中的行// 从参数上看,我们不能直接知道所有列和行的选中信息- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{//    NSString *leftCol;//    NSString *rightCol;////    if (component == 0) {//        // 1. 获取用户当前选中的内容,就是用户用鼠标点的项//        leftCol = _colOne[row];//        //        // 取出第二列选中的行数//        // 通过指定列数,可以获取到其他列当前选中的行数//        // 其他列中的选择情况,需要用代码去获取,不是通过代理方法传递的//        //        // 2. 找第二列当前选中的行数//        NSInteger rightRow = [pickerView selectedRowInComponent:1];//        NSLog(@"第二列选中的行数是 %d", rightRow);//        // 3. 获取第二列当前选中行的内容//        rightCol = _colTwo[rightRow];//    } else {//        // 1. 获取用户当前在第二列选中的内容//        rightCol = _colTwo[row];//        //        // 2. 找到第一列当前选中的行数//        NSInteger leftRow = [pickerView selectedRowInComponent:0];//        NSLog(@"第一列选中的行数是 %d", leftRow);//        // 3. 获取第一列当前选中行的内容//        leftCol = _colOne[leftRow];//    }    NSLog(@"第一列选中的行数是 %d,第二列选中的行数是 %d", [pickerView selectedRowInComponent:0], [pickerView selectedRowInComponent:1]);    NSInteger leftCol = [pickerView selectedRowInComponent:0];    NSInteger rightCol = [pickerView selectedRowInComponent:1];    NSLog(@"%@ ~~~ %@", _colOne[leftCol], _colTwo[rightCol]);}@end

H:/0828/04.选择国旗_ViewController.h
////  ViewController.h//  PickerView-04.选择国旗////  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>@end

H:/0828/04.选择国旗_ViewController.m
//  ViewController.m//  PickerView-04.选择国旗//  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.#import "ViewController.h"@interface ViewController (){    // 选择器的数据源,字典数组    NSArray *_dataList;}@end@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    // 初始化UIPickerView选择器控件    UIPickerView *picker = [[UIPickerView alloc]init];// 1. 设置UIPickerView选择器的数据源为self当前控制器    [picker setDataSource:self];    // 2. 设置UIPickerView选择器的代理为self(当前控制器)    [picker setDelegate:self];    // 3. 设置要显示选中指示器    [picker setShowsSelectionIndicator:YES];    [self.view addSubview:picker];    // 3. 加载选择器数据源    // 3.1 获取完整路径    NSString *path = [[NSBundle mainBundle]pathForResource:@"flags" ofType:@"plist"];//用成员变量记住,字典数组    _dataList = [NSArray arrayWithContentsOfFile:path];    NSLog(@"%@", _dataList);    // 初始化Picker的默认选中第0组的第8行,    [picker selectRow:8 inComponent:0 animated:YES];}#pragma mark - 选择器数据源方法#pragma mark 设置数据组数,即列数- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 1;}#pragma mark 设置第component组的数据的行数- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    return _dataList.count;}#pragma mark - 重点!!!!选择器的代理方法,告诉选择器,每一行要显示的东西是啥样!#pragma mark 重点!!!!用自定义视图填充选择器的内容// row 行// component 列// view 使用做优化的,这儿暂时没有用到!- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{    // 自定义一个UIView    NSInteger viewHeight = 44;    NSInteger labelWidth = 200;    NSInteger viewWidth = 280;    UIView *flagView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight)];    [flagView setBackgroundColor:[UIColor blueColor]];    // 从数组中取出第component列的第row行对应的数据字典    NSDictionary *dict = _dataList[row];    // 1. 填充UIView的内容    // 1.1 国家的名字

H:/0828/05.城市选择_ViewController.h
////  ViewController.h//  PickerView-05.城市选择////  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>@end

H:/0828/05.城市选择_ViewController.m
//  ViewController.m//  PickerView-05.城市选择//  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.#import "ViewController.h"@interface ViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>{    // 选择器数据源    // 1. 省份,数组    NSArray *_province;    // 2. 字典,键是省名,值是数组,数组中元素是一个个城市名    NSMutableDictionary *_city;}@end@implementation ViewController#pragma mark - 私有方法#pragma 自已写的私有方法,用于初始化选择器数据- (void)loadPickerData{    // 1. 省份数组    _province = @[@"北京", @"河北", @"湖南"];    // 2. 城市    // 2.1 初始化城市字典    _city = [NSMutableDictionary dictionary];    // 2.2 实例化城市中的数据    NSArray *city1 = @[@"东城", @"西城"];    [_city setValue:city1 forKey:@"北京"];    NSArray *city2 = @[@"石家庄", @"唐山", @"保定"];    [_city setValue:city2 forKey:@"河北"];    NSArray *city3 = @[@"长沙", @"衡阳", @"郴州"];    [_city setValue:city3 forKey:@"湖南"];}- (void)viewDidLoad{    [super viewDidLoad];    // 1. 初始化PickerView    UIPickerView *picker = [[UIPickerView alloc]init];    // 1.1 设置数据源    [picker setDataSource:self];    // 1.2 设置代理    [picker setDelegate:self];// 1.3 将选择器添加到当前视图    [self.view addSubview:picker];    // 1.4 设置要显示选择指示器    [picker setShowsSelectionIndicator:YES];    // 2. 初始化成员数组和字典的内容    [self loadPickerData];}#pragma mark - 数据源方法#pragma mark 设置2列,前1个component是省名,后1个component是列出所有的城市- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 2;}#pragma mark 数据源方法____设置每1个component的对应行数- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    if (component == 0) {//返回省名组成的数组,成员数目        return _province.count;    } else {//先查看是选中前1列的哪一行        NSInteger rowProvince = [pickerView selectedRowInComponent:0];//再从省名组成的数组中,取出对应的省名        NSString *provinceName = _province[rowProvince];//最后,从字典中根据对应的省名Key,找到对应的Value城市数组,返回城市数组成员数目        NSArray *citys = _city[provinceName];        return citys.count;    }}#pragma mark - 代理方法#pragma mark 重要!!!告诉(设置)选择器每一行要显示的内容- (NSString *)pickerView:(UIPickerView *)pickerView   titleForRow:(NSInteger)row forComponent:(NSInteger)component{    if (component == 0) {//前一列,显示省名数组中的所有省名        return _province[row];    } else {        // 后一列显示对象省名的所有城市数组成员        // 1. 先获得前一列(省份列)选中的行数        NSInteger rowProvince = [pickerView selectedRowInComponent:0];        // 2. 再从省名数组中获得对应省名        NSString *provinceName = _province[rowProvince];        // 3. 最后,从字典中根据键,获得对应的值,即城市的数组        NSArray *citys = _city[provinceName];//        NSLog(@"%@", citys[row]);        // 4. 返回城市数组中row的字符串内容        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 *provinceName = _province[row1];    // 3. 根据键K,获得城市的数组    NSArray *citys = _city[provinceName];NSLog(@"%@~%@", _province[row1], citys[row2]);}@end

H:/0828/06.键盘选择综合_ViewController.h
////  ViewController.h//  PickerView-05.城市选择////  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.//#import <UIKit/UIKit.h>@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>@property (weak, nonatomic) IBOutlet UITextField *nameText;@property (weak, nonatomic) IBOutlet UITextField *phoneText;@property (weak, nonatomic) IBOutlet UITextField *birthdatText;@property (weak, nonatomic) IBOutlet UITextField *cityText;@property (weak, nonatomic) IBOutlet UIButton *photoButton;- (IBAction)selectPhoto:(id)sender;@end

H:/0828/06.键盘选择综合_ViewController.m
//  ViewController.m//  PickerView-05.城市选择//  Created by apple on 13-8-28.//  Copyright (c) 2013年 apple. All rights reserved.#import "ViewController.h"@interface ViewController (){    // 选择器数据源    // 1. 省份,数组,成员是省名    NSArray *_province;    // 2. 城市,字典,key是省名,value是数组,数组成员是一个个城市名    NSDictionary *_city;}@end@implementation ViewController#pragma mark - 私有方法#pragma 自定义私有方法,用于初始化选择器数据- (void)loadPickerData{    // 1. 加载城市定义数据字典    // 1.1 全路径    NSString *path = [[NSBundle mainBundle]pathForResource:@"cities" ofType:@"plist"];    // 1.2 加载字典    NSDictionary *cities = [NSDictionary dictionaryWithContentsOfFile:path];    // 2. 从数据字典中根据key:provinces,获取省名组成的数组    _province = cities[@"provinces"];    // 3. 从数据字典中根据key:cities,读取城市的字典:key是省名,value是数组,数组成员是一个个城市名    _city = cities[@"cities"];}- (void)viewDidLoad{    [super viewDidLoad];    // 1. 初始化PickerView    UIPickerView *picker = [[UIPickerView alloc]init];    // 1.1 设置数据源    [picker setDataSource:self];    // 1.2 设置代理    [picker setDelegate:self];    // 1.3 设置选择指示器    [picker setShowsSelectionIndicator:YES];    // 2. 调用自定义方法,初始化选择器的数据来源的内容    [self loadPickerData];    // 3. 指定城市文本框的输入键盘视图    [_cityText setInputView:picker];    // 4. 处理生日的键盘    // 4.1 初始化日期选择器控件    UIDatePicker *datePicker = [[UIDatePicker alloc]init];    // 4.2 设置日期选择器属性    // 1) 设置选择模式:日期、时间、日期&时间    [datePicker setDatePickerMode:UIDatePickerModeDate];    // 2) 设置日期的区域    [datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_cn"]];    // 3) 增加监听方法    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];    // 4.3 设置生日的输入键盘视图    [_birthdatText setInputView:datePicker];    // 4.4 增加生日键盘的附属视图    UIView *accessView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];    [accessView setBackgroundColor:[UIColor grayColor]];    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];    [button setFrame:CGRectMake(10, 2, 80, 40)];    [button setTitle:@"我爱你" forState:UIControlStateNormal];    [accessView addSubview:button];    [_birthdatText setInputAccessoryView:accessView];}#pragma mark - 代理方法,当日期数值变化时调用代理该方法- (void)dateChanged:(UIDatePicker *)datePicker{    // 需要将日期选择的结果转换成字符串    // 1. 实例化日期格式器    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];    // 2. 设置日期格式器的“格式字符串”    [formatter setDateFormat:@"yyyy-MM-dd"];    // 3. 将日期转换成文本    NSString *dateString = [formatter stringFromDate:datePicker.date];    // 4. 设置生日文本框里面的文字    [_birthdatText setText:dateString];}#pragma mark - 数据源方法#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 rowProvince = [pickerView selectedRowInComponent:0];//再,根据行号作为下标,得到省名数组中的省名        NSString *provinceName = _province[rowProvince];//最后,从字典中根据省名,得到城市名组成的数组        NSArray *citys = _city[provinceName];//返回城市名数组的长度        return citys.count;    }}#pragma mark - 代理方法#pragma mark 设置选择器行的内容的- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{    if (component == 0) {//前一列,为省名        return _province[row];    } else {        // 城市名        // 1. 获得省份列选中的行数        NSInteger rowProvince = [pickerView selectedRowInComponent:0];        // 2. 获得省份名称        NSString *provinceName = _province[rowProvince];        // 3. 获得对应省的由城市名组成的数组        NSArray *citys = _city[provinceName];//        NSLog(@"%@", citys[row]);        // 4. 返回城市数组中row的字符串内容,即城市名        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 *provinceName = _province[row1];    // 3. 获得城市的数组    NSArray *citys = _city[provinceName];    NSLog(@"%@~%@", _province[row1], citys[row2]);    NSString *string = [NSString stringWithFormat:@"%@ %@", _province[row1], citys[row2]];    [_cityText setText:string];}#pragma mark - 完成选择时,将执行照片选择器的代理的该方法- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{    NSLog(@"%@", info);    // 1. 获取编辑后的照片,原图是OriginalImage    UIImage *image = info[@"UIImagePickerControllerEditedImage"];    // 2. 设置按钮的图像    [_photoButton setImage:image forState:UIControlStateNormal];    // 3. 必须手动关闭照片选择控制器,否则它不会自动消失    [self dismissViewControllerAnimated:YES completion:nil];}#pragma mark - Actions#pragma mark 响应按钮点击,选择照片- (IBAction)selectPhoto:(id)sender{    // 1. 实例化照片选择控制器    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];    // 2. 设置照片源    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];    // 3. 设置是否允许编辑    [imagePicker setAllowsEditing:YES];    // 4. 设置代理    [imagePicker setDelegate:self];    // 5. 由于当前控制器,展现照片选择控制器    [self presentViewController:imagePicker animated:YES completion:nil];}@end

H:/0828/07-12键盘处理_KeyboardTool.h
////  KeyboardTool.h//  动画和事件综合例子-键盘处理////  Created by mj on 13-4-16.//  Copyright (c) 2013年 itcast. All rights reserved.//#import <UIKit/UIKit.h>@protocol KeyboardToolDelegate;typedef enum {    kKeyboardToolButtonTypeNext, // 下一个按钮    kKeyboardToolButtonTypePrevious, // 上一个按钮    kKeyboardToolButtonTypeDone // 完成按钮} KeyboardToolButtonType;@interface KeyboardTool : UIToolbar// 按钮@property (nonatomic, readonly) IBOutlet UIBarButtonItem *nextBtn;@property (nonatomic, readonly) IBOutlet UIBarButtonItem *previousBtn;@property (nonatomic, readonly) IBOutlet UIBarButtonItem *doneBtn;// 代理一般用assign策略@property (nonatomic, weak) id<KeyboardToolDelegate> toolDelegate;+ (id)keyboardTool;// 这里写成 - 是为了能在xib中连线- (IBAction)next;- (IBAction)previous;- (IBAction)done;@end@protocol KeyboardToolDelegate <NSObject>- (void)keyboardTool:(KeyboardTool *)tool buttonClick:(KeyboardToolButtonType)type;@end

H:/0828/07-12键盘处理_KeyboardTool.m
////  KeyboardTool.m//  动画和事件综合例子-键盘处理////  Created by mj on 13-4-16.//  Copyright (c) 2013年 itcast. All rights reserved.//#import "KeyboardTool.h"@implementation KeyboardTool#pragma mark 从xib文件中初始化一个KeyboardTool+ (id)keyboardTool {    // owner可以传KeyboardTool这个类    // 点击"下一个"按钮的时候,要调用owner的next方法        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"keyboardTool" owner:nil options:nil];        // 返回初始化完毕的KeyboardTool    return [array lastObject];}#pragma mark - 按钮点击- (void)next {    if ([_toolDelegate respondsToSelector:@selector(keyboardTool:buttonClick:)]) {        [_toolDelegate keyboardTool:self buttonClick:kKeyboardToolButtonTypeNext];    }}- (void)previous {    if ([_toolDelegate respondsToSelector:@selector(keyboardTool:buttonClick:)]) {        [_toolDelegate keyboardTool:self buttonClick:kKeyboardToolButtonTypePrevious];    }}- (void)done {    if ([_toolDelegate respondsToSelector:@selector(keyboardTool:buttonClick:)]) {        [_toolDelegate keyboardTool:self buttonClick:kKeyboardToolButtonTypeDone];    }}@end

H:/0828/07-12键盘处理_MJAppDelegate.h
////  MJAppDelegate.h//  键盘处理////  Created by mj on 13-7-9.//  Copyright (c) 2013年 itcast. All rights reserved.//#import <UIKit/UIKit.h>@interface MJAppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end

H:/0828/07-12键盘处理_MJAppDelegate.m
////  MJAppDelegate.m//  键盘处理////  Created by mj on 13-7-9.//  Copyright (c) 2013年 itcast. All rights reserved.//#import "MJAppDelegate.h"@implementation MJAppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // Override point for customization after application launch.    return YES;}- (void)applicationWillResignActive:(UIApplication *)application{    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application{    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application{    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application{    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application{    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

H:/0828/07-12键盘处理_MJViewController.h
////  MJViewController.h//  键盘处理////  Created by mj on 13-7-9.//  Copyright (c) 2013年 itcast. All rights reserved.//#import <UIKit/UIKit.h>#import "KeyboardTool.h"@interface MJViewController : UIViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate, KeyboardToolDelegate>@property (weak, nonatomic) IBOutlet UITextField *birthdayView;@property (weak, nonatomic) IBOutlet UITextField *cityView;@end

H:/0828/07-12键盘处理_MJViewController.m
//  MJViewController.m//  键盘处理//  Created by mj on 13-7-9.//  Copyright (c) 2013年 itcast. All rights reserved.#import "MJViewController.h"@interface MJViewController ()//所有省名组成的数组@property (nonatomic, strong) NSArray *allProvinces;//字典:省名key---value城市名组成的数组@property (nonatomic, strong) NSDictionary *allCities;//当前激活的活跃状态的输入框@property (nonatomic, weak) UITextField *activeTextField;@property (nonatomic, weak) KeyboardTool *tool;//所有输入框组成的数组@property (nonatomic, strong) NSMutableArray *allTextFields;@end@implementation MJViewController#pragma mark 当日期选择控件的值改变时,回调该方法- (void)dateChange:(UIDatePicker *)picker{    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    formatter.dateFormat = @"yyyy-MM-dd";    self.birthdayView.text = [formatter stringFromDate:picker.date];}- (void)viewDidLoad{    [super viewDidLoad];    self.allTextFields = [NSMutableArray array];    self.tool = [KeyboardTool keyboardTool];//设置键盘工具的代理为当前控制器    self.tool.toolDelegate = self;    // 设置所有文本框的键盘工具条    for (UITextField *field in self.view.subviews)    {//如果不是文本输入框,继续        if (![field isKindOfClass:[UITextField class]]) continue;//每一个文本输入框的键盘工具都是它...        field.inputAccessoryView = self.tool;//数组保存所有的文本输入框,后面要用到        [self.allTextFields addObject:field];        // 设置每个文本输入框的代理都是当前控制器        field.delegate = self;    }    // 设置生日的键盘(不用设置宽高和位置)    UIDatePicker *datePicker = [[UIDatePicker alloc] init];    // 设置区域为中国简体中文    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];    // 只显示日期    datePicker.datePickerMode = UIDatePickerModeDate;    // 监听datePicker的改变    [datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];    self.birthdayView.inputView = datePicker;    // 设置城市的键盘    UIPickerView *picker = [[UIPickerView alloc] init];    picker.dataSource = self;    picker.showsSelectionIndicator = YES;    picker.delegate = self;    self.cityView.inputView = picker;    // 加载省份数据    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities" ofType:@"plist"]];    self.allProvinces = dict[@"provinces"];    self.allCities = dict[@"cities"];}#pragma mark - 重点!!!!!!!!keyboardTool代理方法- (void)keyboardTool:(KeyboardTool *)tool buttonClick:(KeyboardToolButtonType)type{    if (type == kKeyboardToolButtonTypeDone) {//当点击完成时,当前活动的输入框,取消第一响应者,退出键盘        [self.activeTextField resignFirstResponder];    } else {//先取出当前输入框在输入框数组中的索引,        NSUInteger index = [self.allTextFields indexOfObject:self.activeTextField];        if (type == kKeyboardToolButtonTypePrevious) {//当点击上一个时,索引减1,            index--;        } else {//当点击下一个时,索引加1,            index++;        }//取出对应索引的输入框,成为即将成为的第一响应者,调出对应的键盘        UITextField *field = self.allTextFields[index];        [field becomeFirstResponder];    }}#pragma mark - UIPickerView数据源方法#pragma mark 每一列对应多少行- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    if (component == 0) {        return self.allProvinces.count;    } else {        // 获取省份的位置        NSUInteger pIndex = [pickerView selectedRowInComponent:0];        NSString *pName = self.allProvinces[pIndex];        // 获取城市        NSArray *cities = self.allCities[pName];        return cities.count;    }}#pragma mark 一共多少列- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 2;}#pragma mark 每列每行显示什么数据- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{    if (component == 0) {//前一列,显示省名        return self.allProvinces[row];    } else {        // 获取省份的位置        NSUInteger pIndex = [pickerView selectedRowInComponent:0];        NSString *pName = self.allProvinces[pIndex];        // 获取城市        NSArray *cities = self.allCities[pName];        return cities[row];    }}#pragma mark UIPickerView选中了某一行就会调用- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{    // 刷新后一列的数据,联动效果    [pickerView reloadComponent:1];    NSUInteger pIndex = [pickerView selectedRowInComponent:0];    NSString *pName = self.allProvinces[pIndex];    NSUInteger cIndex = [pickerView selectedRowInComponent:1];    NSArray *cities = self.allCities[pName];    NSString *cName = cities[cIndex];    self.cityView.text = [NSString stringWithFormat:@"%@ %@", pName, cName];}#pragma mark - 重点!!!!!!!UITextField代理方法- (void)textFieldDidBeginEditing:(UITextField *)textField{    NSUInteger index = [self.allTextFields indexOfObject:textField];//设置下一个按钮的,是否可用    self.tool.nextBtn.enabled = index != self.allTextFields.count - 1;//设置上一个按钮的,是否可用    self.tool.previousBtn.enabled = index != 0;    // 记住被激活的文本框,其他方法keyboardTool:buttonClick:中要用到    self.activeTextField = textField;}//????????????????????????????????????????????????????- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [self.view endEditing:YES];    return YES;}@end

H:/0828/07-12键盘处理_城市列表cities.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"><dict><key>provinces</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><string>台湾</string><string>天津</string><string>西藏</string><string>香港</string><string>新疆</string><string>云南</string><string>浙江</string></array><key>cities</key><dict><key>北京</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>重庆</key><array><string>重庆</string></array><key>上海</key><array><string>上海</string></array><key>天津</key><array><string>天津</string></array><key>安徽</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>福建</key><array><string>福州</string><string>龙岩</string><string>南平</string><string>宁德</string><string>莆田</string><string>泉州</string><string>三明</string><string>厦门</string><string>永安</string><string>漳州</string></array><key>广东</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>甘肃</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>广西</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>贵州</key><array><string>贵阳</string><string>安顺</string><string>毕节</string><string>都匀</string><string>凯里</string><string>六盘水</string><string>铜仁</string><string>兴义</string><string>遵义</string></array><key>河北</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>河南</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>黑龙江</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>湖北</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>湖南</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>吉林</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>江苏</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>江西</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>辽宁</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>内蒙古</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>宁夏</key><array><string>银川</string><string>固原</string><string>石嘴山</string><string>吴忠</string><string>中卫</string></array><key>青海</key><array><string>西宁</string><string>果洛</string><string>海北</string><string>海东</string><string>海南</string><string>海西</string><string>黄南</string><string>玉树</string></array><key>四川</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>山东</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>陕西</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>山西</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>新疆</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>西藏</key><array><string>拉萨</string><string>阿里</string><string>昌都</string><string>林芝</string><string>那曲</string><string>日喀则</string><string>山南</string></array><key>云南</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>浙江</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>澳门</key><array><string>澳门</string></array><key>台湾</key><array><string>台北</string><string>高雄</string><string>台南</string><string>台中</string></array><key>香港</key><array><string>香港</string></array><key>海南</key><array><string>海口</string><string>儋州</string><string>东方</string><string>琼海</string><string>三亚</string><string>万宁</string><string>文昌</string><string>五指山</string></array></dict></dict></plist>

0 0