最終效果圖:

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPjwvcD4KPHAgY2xhc3M9"p1">BeyondViewController.h
// // BeyondViewController.h // 9_鍵盤的簡單處理 // // Created by beyond on 14-7-25. // Copyright (c) 2014年 com.beyond. All rights reserved. // #import@interface BeyondViewController : UIViewController - (IBAction)exitKeyboard:(UIButton *)sender; @end
BeyondViewController.m
//
// BeyondViewController.m
// 9_鍵盤的簡單處理
/*
存在的問題:
1,彈出的鍵盤可能會遮住界面上的控件,解決方法:使用scrollView,或,動態減少控件的Y值(使之上移)
2,toolBar上面的兩個按鈕的點擊事件,還沒有實現
*/
// Created by beyond on 14-7-25.
// Copyright (c) 2014年 com.beyond. All rights reserved.
//
#import "BeyondViewController.h"
@interface BeyondViewController ()
{
// 鍵盤上面的附屬工具條
UIToolbar *_toolBar;
}
@end
@implementation BeyondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 為所有鍵盤上面添加工具條
[self addToolBarForKeyboard];
}
// 為所有鍵盤上面添加工具條
- (void)addToolBarForKeyboard
{
// mainBundel加載xib,擴展名不用寫.xib
NSArray *arrayXibObjects = [[NSBundle mainBundle] loadNibNamed:@"KeyToolBar" owner:nil options:nil];
// 鍵盤附屬工具條
_toolBar = arrayXibObjects[0];
// 為self.view內部的所有文本輸入框設置toolBar
NSArray *array = self.view.subviews;
for (UIView *obj in array) {
if ([obj isKindOfClass:[UITextField class]]) {
// 為什麼要強轉,因為UIView的屬性 inputAccessoryView 是readOnly
UITextField *obj2 = (UITextField *)obj;
obj2.inputAccessoryView = _toolBar;
}
}
// 為toolBar中的第1個UIToolbarTextButton(上一個按鈕)添加點擊事件
//[[_toolBar.subviews firstObject] addTarget:self action:@selector(previousKeyboard:) forControlEvents:UIControlEventTouchUpInside];
// 為toolBar中的第2個UIToolbarTextButton(下一個按鈕)添加點擊事件
//[[_toolBar.subviews objectAtIndex:2] addTarget:self action:@selector(nextKeyboard:) forControlEvents:UIControlEventTouchUpInside];
// 為toolBar中的最後一個UIToolbarTextButton(完成按鈕)添加點擊事件
[[_toolBar.subviews lastObject] addTarget:self action:@selector(exitKeyboard:) forControlEvents:UIControlEventTouchUpInside];
}
// toolBar裡面,點擊上一個按鈕
- (void)previousKeyboard:(UIButton *)sender
{
NSLog(@"點擊了上一個按鈕,要激活上一個輸入框");
}
// toolBar裡面,點擊下一個按鈕
- (void)nextKeyboard:(UIButton *)sender
{
NSLog(@"點擊了下一個按鈕,要激活下一個輸入框");
}
// 退出鍵盤
- (IBAction)exitKeyboard:(UIButton *)sender {
// 方式1: self.view內部所有的文本框(包括子孫控件...)都退出第一響應者
[self.view endEditing:YES];
return;
// 方式2:
// 遍歷uiview裡面所有的控件 ,resignFirstResponder
/*
for (int i=0; i
KeyToolBar.xib
