1 新建一个工具类
#import <UIKit/UIKit.h>
@class KeyboardTool;
typedef enum
{
kKeyboardToolButtonPrev = 0,
kKeyboardToolButtonNext,
kKeyboardToolButtonDone
} KeyboardToolButtonType;
#pragma mark - 定义协议
@protocol KeyboardToolDelegate <NSObject>
- (void)keyboardTool:(KeyboardTool *)keyboard buttonType:(KeyboardToolButtonType)buttonType;
@end
#pragma mark - 定义接口
@interface KeyboardTool : UIToolbar
#pragma mark 定义代理
@property (weak, nonatomic) id<KeyboardToolDelegate> toolDelegate;
// 工厂方法,用于简化对象的实例化
// 工厂方法,通常与类名相同,首字母小写
// 工厂方法的参数可以使用WithXXX的格式传递
+ (id)keyboardTool;
// 上一个按钮
@property (weak, nonatomic) IBOutlet UIBarButtonItem *prevButton;
// 下一个按钮
@property (weak, nonatomic) IBOutlet UIBarButtonItem *nextButton;
// 上一个
- (IBAction)clickPrev:(UIBarButtonItem *)sender;
// 下一个
- (IBAction)clickNext:(UIBarButtonItem *)sender;
// 完成
- (IBAction)clickDone:(UIBarButtonItem *)sender;
@end
#import "KeyboardTool.h"
@implementation KeyboardTool
#pragma mark 实例化助手视图
+ (id)keyboardTool
{
// 从XIB中加载一组平行的视图数组,然后从中找到定义好的键盘助手视图
// 在本示例中,只有一个视图
NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"KeyboardTool" owner:nil options:nil];
return array.lastObject;
}
#pragma mark - 按钮操作
- (IBAction)clickPrev:(UIBarButtonItem *)sender
{
[_toolDelegate keyboardTool:self buttonType:kKeyboardToolButtonPrev];
}
- (IBAction)clickNext:(id)sender
{
[_toolDelegate keyboardTool:self buttonType:kKeyboardToolButtonNext];
}
- (IBAction)clickDone:(UIBarButtonItem *)sender
{
[_toolDelegate keyboardTool:self buttonType:kKeyboardToolButtonDone];
}
@end
#import <UIKit/UIKit.h>
#import "KeyboardTool.h"
@interface MainViewController : UIViewController <KeyboardToolDelegate, UITextFieldDelegate>
@end
#import "MainViewController.h"
@interface MainViewController ()
// 键盘工具自定义视图
@property (weak, nonatomic) KeyboardTool *keyboardTool;
// 建立所有文本输入控件的数组
@property (strong, nonatomic) NSArray *textFiledArray;
// 用户选中的文本框
@property (weak, nonatomic) UITextField *selectedTextField;
@end
@implementation MainViewController
#pragma mark - 初始化界面
- (void)loadView
{
// 1. 实例化视图
self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
// 2. 创建UILabel
[self createLabelWithFrame:CGRectMake(20, 20, 80, 30) text:@"姓名"];
[self createLabelWithFrame:CGRectMake(20, 56, 80, 30) text:@"QQ"];
[self createLabelWithFrame:CGRectMake(20, 92, 80, 30) text:@"生日"];
[self createLabelWithFrame:CGRectMake(20, 128, 80, 30) text:@"城市"];
// 3. 创建TextField
// 注意:因为文本空间需要使用keyboardTool作为助手视图,
// 因此,在定义文本空间的助手视图之前,需要实例化keyboardTool视图
self.keyboardTool = [KeyboardTool keyboardTool];
[self.keyboardTool setToolDelegate:self];
UITextField *nameText = [self createTextFiledWithFrame:CGRectMake(105, 20, 195, 30) placeHolder:@"请输入姓名"];
[nameText setInputAccessoryView:self.keyboardTool];
UITextField *qqText = [self createTextFiledWithFrame:CGRectMake(105, 56, 195, 30) placeHolder:@"请输入QQ"];
[qqText setInputAccessoryView:self.keyboardTool];
UITextField *birthdayText = [self createTextFiledWithFrame:CGRectMake(105, 92, 195, 30) placeHolder:@"请选择生日"];
[birthdayText setInputAccessoryView:self.keyboardTool];
UITextField *cityText = [self createTextFiledWithFrame:CGRectMake(105, 128, 195, 30) placeHolder:@"请选择城市"];
[cityText setInputAccessoryView:self.keyboardTool];
// 实例化文本框数组
self.textFiledArray = @[nameText, qqText, birthdayText, cityText];
// // 常用遍历视图中子视图控件,并查找其中指定类型的方法
// for (UIView *textField in self.view.subviews) {
// // 判断遍历的控件类型是否是文本框控件
// if ([textField isKindOfClass:[UITextField class]]) {
// [self.textFiledArray addObject:textField];
// }
// }
NSLog(@"%@", self.textFiledArray);
}
#pragma mark - 私有方法
#pragma mark 创建TextField
- (UITextField *)createTextFiledWithFrame:(CGRect)frame placeHolder:(NSString *)placeHolder
{
UITextField *textField = [[UITextField alloc]initWithFrame:frame];
// 1)设置边框
[textField setBorderStyle:UITextBorderStyleRoundedRect];
// 2) 设置垂直居中排列
[textField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
// 3) 文本的占位提示信息
[textField setPlaceholder:placeHolder];
// 4) 将文本添加到根视图
[self.view addSubview:textField];
// 5 添加文本框的代理,以便光标进入文本框时记录
[textField setDelegate:self];
return textField;
}
#pragma mark 创建UILabel
- (void)createLabelWithFrame:(CGRect)frame text:(NSString *)text
{
UILabel *label = [[UILabel alloc]initWithFrame:frame];
[label setText:text];
[self.view addSubview:label];
}
#pragma mark UITextField代理方法
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// 记录当前选中的文本框
self.selectedTextField = textField;
NSUInteger index = [self.textFiledArray indexOfObject:textField];
// 如果是数组中第一个文本框,禁用上一个按钮
self.keyboardTool.prevButton.enabled = (index != 0);
// 如果是数组中最后一个文本框,禁用下一个按钮
self.keyboardTool.nextButton.enabled = (index != self.textFiledArray.count - 1);
}
#pragma mark 键盘助手视图代理方法
- (void)keyboardTool:(KeyboardTool *)keyboard buttonType:(KeyboardToolButtonType)buttonType
{
/**
上一个&下一个文本控件的切换
*/
NSLog(@"%d", buttonType);
if (kKeyboardToolButtonDone == buttonType) {
// 关闭键盘
[self.view endEditing:YES];
} else {
// 1. 获取当前选中的文本控件
// 2. 获取当前空间在数组中的索引
NSUInteger index = [self.textFiledArray indexOfObject:self.selectedTextField];
if (kKeyboardToolButtonNext == buttonType) {
index++;
} else {
index--;
}
UITextField *textField = self.textFiledArray[index];
[textField becomeFirstResponder];
}
}
@end