足球游戏_中国足彩网¥体育资讯$

iphone开发中给键盘加个隐藏工具条
来源:易贤网 阅读:686 次 日期:2014-11-14 14:21:16
温馨提示:易贤网小编为您整理了“iphone开发中给键盘加个隐藏工具条”,方便广大网友查阅!

因为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技术专栏

中国足彩网信息请查看技术文章
易贤网手机网站地址:iphone开发中给键盘加个隐藏工具条
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标