Because of the touch involved with the iphone, there is no hardware keyboard, the general is to click the input box, pop a virtual keyboard out, so in the iphone development, often in the completion of editing input, to write program code to turn off the soft keyboard output, very cumbersome, of course, there are many ways to close the soft keyboard, For example, put a button on the bottom, by clicking on the blank of the screen to close the keyboard; You can also handle the return keyboard event to turn off the keyboard, these aside, this article is to share a keyboard top of the bar of the class, through this toolbar, you can easily close the keyboard, and have the previous item, the next entry box switch, Very convenient, the effect please look at the following figure:
The class files are as follows:
KeyBoardTopBar.h
//
KeyBoardTopBar.h
//
//
Created by Walkman on 10-12-2.
Copyright 2010 Mobile Theme Http://www.shouji138.com All rights reserved.
//
#import
@interface Keyboardtopbar:nsobject {
Uitoolbar *view;//Tool Bar
Nsarray *textfields;//Input Box array
BOOL allowshowpreandnext;//whether to display the next item in the previous item
BOOL isinnavigationcontroller;//whether in Navigation view
Uibarbuttonitem *prevbuttonitem;//Previous button
Uibarbuttonitem *nextbuttonitem;//Next button
Uibarbuttonitem *hiddenbuttonitem;//Hide Button
Uibarbuttonitem *spacebuttonitem;//Blank button
Uitextfield *currenttextfield;//Current Input box
}
@property (Nonatomic,retain) Uitoolbar *view;
-(ID) init; Class
-(void) Setallowshowpreandnext: (BOOL) isshow; Sets whether to display the next item in the previous item
-(void) Setisinnavigationcontroller: (BOOL) Isbool; Set whether in Navigation view
-(void) Settextfieldsarray: (Nsarray *) array; Set an array of input boxes
-(void) showprevious; Displays the previous item
-(void) shownext; Display the next item
-(void) Showbar: (Uitextfield *) TextField; Show the tool bar
-(void) Hiddenkeyboard; Hide keyboard
@end
KEYBOARDTOPBAR.M file
//
Keyboardtopbar.m
//
Created by Walkman on 10-12-2.
Copyright 2010 Mobile Theme Download http://www.shouji138.com All rights reserved.
//
#import "KeyBoardTopBar.h"
@implementation Keyboardtopbar
@synthesize view;
Initializing controls and variables
-(ID) init{
if (self = [super init]) {
Prevbuttonitem = [[Uibarbuttonitem alloc] initwithtitle:@ "Previous" Style:uibarbuttonitemstylebordered target:self action: @selector (showprevious)];
Nextbuttonitem = [[Uibarbuttonitem alloc] initwithtitle:@ "Next" style:uibarbuttonitemstylebordered action: @selector (Shownext)];
Hiddenbuttonitem = [[Uibarbuttonitem alloc] initwithtitle:@ "Hide Keyboard" 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;
}
Set whether in Navigation view
-(void) Setisinnavigationcontroller: (BOOL) isbool{
Isinnavigationcontroller = Isbool;
}
Displays the previous item
-(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]];
}
}
Display the next item
-(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]];
}
}
Show the tool bar
-(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];
}
Set an array of input boxes
-(void) Settextfieldsarray: (Nsarray *) array{
Textfields = array;
}
Sets whether the previous and Next buttons are displayed
-(void) Setallowshowpreandnext: (BOOL) isshow{
Allowshowpreandnext = Isshow;
}
Hide Keyboard and tool bars
-(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];
}
Release
-(void) Dealloc {
[View release];
[Textfields release];
[Prevbuttonitem release];
[Nextbuttonitem release];
[Hiddenbuttonitem release];
[Currenttextfield release];
[Spacebuttonitem release];
[Super Dealloc];
}
@end
Here is the code that uses this class:
Declare in Uiviewcontroller header file and define and implement Uitextfielddelegate proxy
For example: In the KeyboardtopbarViewController.h file, that's what I wrote.
//
KeyboardtopbarViewController.h
Keyboardtopbar
//
Created by Walkman on 10-12-2.
Copyright 2010 Mobile Theme 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;
}
In Uiviewcontroller m file, initialize and add to view
-(void) Viewdidload {
[Super Viewdidload];
......
Where Editfieldarray is the Uitextfield array, which has been initialized above and added n a uitextfield inside.
The specific code please download the attachment view, here only posted the related code
Keyboardbar = [[Keyboardtopbar alloc]init];
[Keyboardbar Setallowshowpreandnext:yes];
[Keyboardbar Setisinnavigationcontroller:no];
[Keyboardbar Settextfieldsarray:editfieldarray];
[Self.view AddSubview:keyboardbar.view];
}
This method is the method in the Uitextfielddelegate proxy, which means that the input box begins to be in edit state.
-(void) textfielddidbeginediting: (Uitextfield *) textfield{
[Keyboardbar Showbar:textfield]; Show the tool bar
......
}
OK, the call is still very convenient, of course, this class still need to improve the place, for example, after the implementation of the Hiddenkeyboard method hides the keyboard and toolbar, if you need to do further processing in the call page, currently is not possible, And then add a delegate class to the next version.