因为iphone手机采用的触摸涉及,本身没有硬件键盘,一般都是点击输入框之后,弹出一个虚拟键盘出来,因此在iphone开发中,经常在完成编辑输入之后,要写程序代码来关闭软键盘的输出,非常繁琐,当然关闭软键盘的方式有很多,比如放一个按钮在底层,通过点击屏幕的空白处来关闭键盘;也可以处理return键盘事件来关闭键盘,这些暂且不说,本文要分享的是一个键盘顶部工具条的类,通过这个工具条,可以很方便的关闭键盘,而且有上一项,下一项的输入框切换,非常方便,效果请看下图:
名单
类文件如下:
keyboardtopbar.h
//
// keyboardtopbar.h
//
//
// created by walkman on 10-12-2.
// copyright 2010 手机主题 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 手机主题下载 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 手机主题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技术专栏
2025国考·省考课程试听报名