键盘处理例子思路
步骤1:简单搭建界面
步骤2、自定义XIB,用来描写性别
+ (id)sexBox
{
return [[NSBundle mainBundle] loadNibNamed:@"MJSexBox"
owner:nil
options:nil][0];
}
2.1监听性别按钮点击,修改按钮的状态
#pragma mark
改变了性别选择
- (IBAction)sexChange {
if (_manBtn.enabled) {
//
点击了男的
_manBtn.enabled =
NO;
_womanBtn.enabled =
YES;
}
else {
//
点击了女的
_manBtn.enabled =
YES;
_womanBtn.enabled =
NO;
}
}
2.2.在控制器中添加性别选择控件
MJSexBox *sexBox = [MJSexBox sexBox];
sexBox.center = CGPointMake(150, 70);
[self.view addSubview:sexBox];
步骤3:自定义键盘
3.1.设置生日键盘
// 1.1.生日
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
//
只显示日期5
datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
[datePicker addTarget:self
action:@selector(birthdayChange:) forControlEvents:UIControlEventValueChanged];
_birthdayField.inputView = datePicker;
//
设置键盘为日期选择控件
_birthdayField.delegate =
self;
3.2制止生日键盘输入文字
#pragma mark - UITextField代理
#pragma mark
每当用户输入文字的时候就会调用这个方法,返回NO,制止输入;但会YES,允许输入
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
return
NO;
}
3.3
监听生日键盘的值改变事件
#pragma mark -
生日改变
- (void)birthdayChange:(UIDatePicker *)picker
{
// 1.获得当前时间
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat =
@"yyyy-MM-dd";
NSString *time = [fmt stringFromDate:picker.date];
// 2.赋值到文本框
_birthdayField.text = time;
}
步骤4、设置城市
4.1用xib描写城市键盘,并且设置UIPickerView的代理和数据源,创建自定义视图。
+ (id)cityPicker
{
return [[NSBundle mainBundle] loadNibNamed:@"MJCityPicker"
owner:nil
options:nil][0];
}
4.2加载数据,在awakeFromNib中调用
#pragma mark
任何对象从xib中创建终了的时候都会调用1次
- (void)awakeFromNib
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities.plist"
ofType:nil]];
_provinces = [NSMutableArray array];
for (NSDictionary *dict
in array) {
MJProvince *p = [MJProvince provinceWithDict:dict];
[_provinces addObject:p];
}
}
4.3实现数据源和代理方法。
#pragma mark - UIPickerView数据源方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return
2;
}
#pragma mark
第component列有多少行数据
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component ==
0) {
//
多少个省分
return _provinces.count;
}
else {
//
当前选中省分的行数(城市个数)
// 1.取得选中了哪个省
int pIndex = [pickerView selectedRowInComponent:0];
// 2.取出省分模型
MJProvince *p = _provinces[pIndex];
// 3.返回当前省分城市的个数
return p.cities.count;
}
}
#pragma mark - UIPickerView代理方法
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component ==
0) {
//显示哪一个省分
// 1.取出省分模型
MJProvince *p = _provinces[row];
// 2.取出省分名称
return p.name;
}
else {
//
显示哪一个城市
// 1.取得选中了哪个省
int pIndex = [pickerView selectedRowInComponent:0];
// 2.取出省分模型
MJProvince *p = _provinces[pIndex];
// 3.返回对应行的城市名称
return p.cities[row];
}
}
#pragma mark
监听选中了某1列的某1行
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component ==
0) {
//
改变了省分
//
刷新第1列的数据(重新刷新数据,重新调用数据源和代理的相应方法取得数据)
[pickerView reloadComponent:1];
//
选中第1列的第0行
[pickerView selectRow:0
inComponent:1
animated:YES];
}
//
更改文字
// 1.取得选中的省分名称
int pIndex = [pickerView selectedRowInComponent:0];
MJProvince *p = _provinces[pIndex];
// 2.取得选中的城市位置
int cIndex = [pickerView selectedRowInComponent:1];
// 3.通知代理
if ([_delegate respondsToSelector:@selector(cityPicker:citySelectWithProvince:city:)])
{
[_delegate cityPicker:self
citySelectWithProvince:p.name city:p.cities[cIndex]];
}
}
4.4调用自定义视图设置城市键盘
设置城市
MJCityPicker *cityPicker = [MJCityPicker cityPicker];
cityPicker.delegate =
self;
_cityField.inputView = cityPicker;
//
设置键盘为pickerview
_cityField.delegate =
self;
#pragma mark - MJCityPicker代理方法
#pragma mark
选中了某个城市就会调用
- (void)cityPicker:(MJCityPicker *)cityPicker
citySelectWithProvince:(NSString *)province city:(NSString *)city
{
_cityField.text = [NSString stringWithFormat:@"%@ %@", province, city];
}
4.5
给自定义城市键盘声明1个协议,并添加1个代理属性,当转动键盘的时候,通知代理做些事情.
@protocol
MJCityPickerDelegate <NSObject]]>
@optional
- (void)cityPicker:(MJCityPicker *)cityPicker citySelectWithProvince:(NSString *)province city:(NSString
*)city;
@end
4.6
在控制器中实现代理协议的方法。
#pragma mark - MJCityPicker代理方法
#pragma mark
选中了某个城市就会调用
- (void)cityPicker:(MJCityPicker *)cityPicker
citySelectWithProvince:(NSString *)province city:(NSString *)city
{
_cityField.text = [NSString stringWithFormat:@"%@ %@", province, city];
}
步骤5:工具条
5.1自定义1个xib描写工具条
+ (id)keyboardTool
{
return [[NSBundle mainBundle] loadNibNamed:@"MJKeyboardTool"
owner:nil
options:nil][0];
}
5.2将toolbar用view包装起来,目的不让外界修改toolbar,由于外界拿到的是UIView,而不是toolbar,就不能直接获得toolbar里面的属性了。
5.3自定义1个自定义工具条类,和xib绑定。
- (IBAction)previous;
//
上1个
- (IBAction)next;
//
下1个
- (IBAction)done;
//
完成
5.4 定义1个协议,通知代理按钮点击事件
@optional
- (void)keyboardTool:(MJKeyboardTool *)keyboardTool itemClick:(MJKeyboardToolItemType)itemType;
@end
5.5定义1个枚举类型,辨别按钮
typedef
enum {
MJKeyboardToolItemTypePrevious,
//
上1个
MJKeyboardToolItemTypeNext,
//
下1个
MJKeyboardToolItemTypeDone
//
完成
} MJKeyboardToolItemType;
5.6当点击按钮时,通知代理
#pragma mark
上1个
- (void)previous
{
//
通知代理(上1个按钮被点击了)
if ([_delegate respondsToSelector:@selector(keyboardTool:itemClick:)])
{
[_delegate keyboardTool:self
itemClick:MJKeyboardToolItemTypePrevious];
}
}
#pragma mark
下1个
- (void)next
{
//
通知代理(下1个按钮被点击了)
if ([_delegate respondsToSelector:@selector(keyboardTool:itemClick:)])
{
[_delegate keyboardTool:self
itemClick:MJKeyboardToolItemTypeNext];
}
}
#pragma mark
完成
- (void)done
{
//
通知代理(完成按钮被点击了)
if ([_delegate respondsToSelector:@selector(keyboardTool:itemClick:)])
{
[_delegate keyboardTool:self
itemClick:MJKeyboardToolItemTypeDone];
}
}
5.7 让控制器作为工具条的代理,并实现工具条代理方法
#pragma mark - MJKeyboardTool代理方法
#pragma mark
点击了工具条上面的按钮就会调用
- (void)keyboardTool:(SUNKeyboardTool
*)keyboardTool itemClick:(SUNKeyboardToolItemType)itemType
{
if (itemType ==
doneKeyboardToolItemType) {
[self.view
endEditing:YES];
}else{
int index = [_fields
indexOfObject:_focusedField];
if (itemType ==
previousKeyboardToolItemType) {
index--;
}else{
index++;
}
//
变成第1响应者
[_fields[index]
becomeFirstResponder];
}
}
5.8
监听所有文本框的开始编辑,设置所有文本框的代理为控制器
// 3.取得所有的文本输入框
MJKeyboardTool *tool = [MJKeyboardTool keyboardTool];
tool.delegate =
self;
for
(UIView *child
in
self.view.subviews) {
//
如果是文本输入框,就设置工具条
if ([child isKindOfClass:[UITextField class]]) {
UITextField *field = (UITextField *)ch
------分隔线----------------------------
------分隔线----------------------------