萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> ios >> iPhone開發中給鍵盤加個隱藏工具條

iPhone開發中給鍵盤加個隱藏工具條

   因為iPhone手機采用的觸摸涉及,本身沒有硬件鍵盤,一般都是點擊輸入框之後,彈出一個虛擬鍵盤出來,因此在iPhone開發中,經常在完成編輯輸入之後,要寫程序代碼來關閉軟鍵盤的輸出,非常繁瑣,當然關閉軟鍵盤的方式有很多,比如放一個按鈕在底層,通過點擊屏幕的空白處來關閉鍵盤;也可以處理Return鍵盤事件來關閉鍵盤,這些暫且不說,本文要分享的是一個鍵盤頂部工具條的類,通過這個工具條,可以很方便的關閉鍵盤,而且有上一項,下一項的輸入框切換,非常方便,效果請看下圖:

iPhone開發中給鍵盤加個隱藏工具條 三聯

  類文件如下:

  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

copyright © 萬盛學電腦網 all rights reserved