因为iPhone手机采用的触摸涉及,本身没有硬件键盘,一般都是点击输入框之后,弹出一个虚拟键盘出来,因此在iPhone开发中,经常在完成编辑输入之后,要写程序代码来关闭软键盘的输出,非常繁琐,当然关闭软键盘的方式有很多,比如放一个按钮在底层,通过点击屏幕的空白处来关闭键盘;也可以处理Return键盘事件来关闭键盘,这些暂且不说,本文要分享的是一个键盘顶部工具条的类,通过这个工具条,可以很方便的关闭键盘,而且有上一项,下一项的输入框切换,非常方便,效果请看下图:
类文件如下:
KeyBoardTopBar.h
//
// KeyBoardTopBar.h
//
//
// Created by walkman on 10-12-2.
// Copyright 2010 手机主题 http://www.shouji138.com All rights reserved.
//
#import
@interface KeyBoardTopBar : NSObject {
UIToolbar *view;//工具条
NSArray *TextFields;//输入框数组
BOOL allowShowPreAndNext;//是否显示上一项下一项
BOOL isInNavigationController;//是否在导航视图中
UIBarButtonItem *prevButtonItem;//上一项按钮
UIBarButtonItem *nextButtonItem;//下一项按钮
UIBarButtonItem *hiddenButtonItem;//隐藏按钮
UIBarButtonItem *spaceButtonItem;//空白按钮
UITextField *currentTextField;//当前输入框
}
@property(nonatomic,retain) UIToolbar *view;
-(id)init; //初始化
-(void)setAllowShowPreAndNext:(BOOL)isShow; //设置是否显示上一项下一项
-(void)setIsInNavigationController:(BOOL)isbool; //设置是否在导航视图中
-(void)setTextFieldsArray:(NSArray *)array; //设置输入框数组
-(void)ShowPrevious; //显示上一项
-(void)ShowNext; //显示下一项
-(void)ShowBar:(UITextField *)textField; //显示工具条
-(void)HiddenKeyBoard; //隐藏键盘
@end
KeyBoardTopBar.m 文件
//
// KeyBoardTopBar.m
//
// Created by walkman on 10-12-2.
// Copyright 2010 手机主题下载 http://www.shouji138.com All rights reserved.
//
#import "KeyBoardTopBar.h"
@implementation KeyBoardTopBar
@synthesize view;
//初始化控件和变量
-(id)init{
if(self = [super init]) {
prevButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"上一项" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowPrevious)];
nextButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一项" style:UIBarButtonItemStyleBordered target:self action:@selector(ShowNext)];
hiddenButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏键盘" style:UIBarButtonItemStyleBordered target:self action:@selector(HiddenKeyBoard)];
spaceButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
view = [[UIToolbar alloc] initWithFrame:CGRectMake(0,480,320,44)];
view.barStyle = UIBarStyleBlackTranslucent;
view.items = [NSArray arrayWithObjects:prevButtonItem,nextButtonItem,spaceButtonItem,hiddenButtonItem,nil];
allowShowPreAndNext = YES;
TextFields = nil;
isInNavigationController = YES;
currentTextField = nil;
}
return self;
}
//设置是否在导航视图中
-(void)setIsInNavigationController:(BOOL)isbool{
isInNavigationController = isbool;
}
//显示上一项
-(void)ShowPrevious{
if (TextFields==nil) {
return;
}
NSInteger num = -1;
for (NSInteger i=0; i<[TextFields count]; i++) {
if ([TextFields objectAtIndex:i]==currentTextField) {
num = i;
break;
}
}
if (num>0){
[[TextFields objectAtIndex:num] resignFirstResponder];
[[TextFields objectAtIndex:num-1 ] becomeFirstResponder];
[self ShowBar:[TextFields objectAtIndex:num-1]];
}
}
//显示下一项
-(void)ShowNext{
if (TextFields==nil) {
return;
}
NSInteger num = -1;
for (NSInteger i=0; i<[TextFields count]; i++) {
if ([TextFields objectAtIndex:i]==currentTextField) {
num = i;
break;
}
}
if (num<[TextFields count]-1){
[[TextFields objectAtIndex:num] resignFirstResponder];
[[TextFields objectAtIndex:num+1] becomeFirstResponder];
[self ShowBar:[TextFields objectAtIndex:num+1]];
}
}
//显示工具条
-(void)ShowBar:(UITextField *)textField{
currentTextField = textField;
if (allowShowPreAndNext) {
[view setItems:[NSArray arrayWithObjects:prevButtonItem,nextButtonItem,spaceButtonItem,hiddenButtonItem,nil]];
}
else {
[view setItems:[NSArray arrayWithObjects:spaceButtonItem,hiddenButtonItem,nil]];
}
if (TextFields==nil) {
prevButtonItem.enabled = NO;
nextButtonItem.enabled = NO;
}
else {
NSInteger num = -1;
for (NSInteger i=0; i<[TextFields count]; i++) {
if ([TextFields objectAtIndex:i]==currentTextField) {
num = i;
break;
}
}
if (num>0) {
prevButtonItem.enabled = YES;
}
else {
prevButtonItem.enabled = NO;
}
if (num<[TextFields count]-1) {
nextButtonItem.enabled = YES;
}
else {
nextButtonItem.enabled = NO;
}
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
if (isInNavigationController) {
view.frame = CGRectMake(0, 201-40, 320, 44);
}
else {
view.frame = CGRectMake(0, 201, 320, 44);
}
[UIView commitAnimations];
}
//设置输入框数组
-(void)setTextFieldsArray:(NSArray *)array{
TextFields = array;
}
//设置是否显示上一项和下一项按钮
-(void)setAllowShowPreAndNext:(BOOL)isShow{
allowShowPreAndNext = isShow;
}
//隐藏键盘和工具条
-(void)HiddenKeyBoard{
if (currentTextField!=nil) {
[currentTextField resignFirstResponder];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
view.frame = CGRectMake(0, 480, 320, 44);
[UIView commitAnimations];
}
//释放
- (void)dealloc {
[view release];
[TextFields release];
[prevButtonItem release];
[nextButtonItem release];
[hiddenButtonItem release];
[currentTextField release];
[spaceButtonItem release];
[super dealloc];
}
@end
下面是使用这个类的代码:
在UIViewController头文件中申明,并定义,并且实现UITextFieldDelegate代理
比如:在keyboardtopbarViewController.h文件,我是这样写的
//
// keyboardtopbarViewController.h
// keyboardtopbar
//
// Created by walkman on 10-12-2.
// Copyright 2010 手机主题 http://www.shouji138.com All rights reserved.
//
#import
@class KeyBoardTopBar;
@interface keyboardtopbarViewController : UIViewController {
UITableView *tableview;
NSMutableArray *cellsTextArray;
NSMutableArray *editFieldArray;
UIButton *btnReg;
KeyBoardTopBar *keyboardbar;
CGRect rect;
}
在在UIViewController的m文件中,初始化,并添加到view中
- (void)viewDidLoad {
[super viewDidLoad];
......
//其中editFieldArray 是UITextField数组,在上面已经初始化,并添加了N个UITextField在里面。
//具体的代码请下载附件查看,这里只贴出了相关的代码
keyboardbar = [[KeyBoardTopBar alloc]init];
[keyboardbar setAllowShowPreAndNext:YES];
[keyboardbar setIsInNavigationController:NO];
[keyboardbar setTextFieldsArray:editFieldArray];
[self.view addSubview:keyboardbar.view];
}
//这个方法是UITextFieldDelegate代理中的方法,表示输入框开始处于编辑状态。
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[keyboardbar ShowBar:textField]; //显示工具条
......
}
OK了,调用起来还是很方便吧,当然,这个类还有需要完善的地方,比如,在执行了HiddenKeyBoard方法隐藏了键盘和工具条之后,如果在调用页面时候需要再做进一步处理,目前是无法实现的,等下一个版本中再加入一个Delegate类。
中国足彩网信息请查看IT技术专栏