最后效果图:

BeyondViewController.h

//
// BeyondViewController.h
// 9_键盘的简单处理
//
// Created by beyond on 14-7-25.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// #import <span style="font-family: Arial, Helvetica, sans-serif;"><UIKit/UIKit.h></span> @interface BeyondViewController : UIViewController
- (IBAction)exitKeyboard:(UIButton *)sender; @end </uikit>

BeyondViewController.m

//
// BeyondViewController.m
// 9_键盘的简单处理
/*
存在的问题:
1,弹出的键盘可能会遮住界面上的控件,解决方法:使用scrollView,或,动态降低控件的Y值(使之上移)
2,toolBar上面的两个button的点击事件,还没有实现
*/
// 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(上一个button)加入点击事件
//[[_toolBar.subviews firstObject] addTarget:self action:@selector(previousKeyboard:) forControlEvents:UIControlEventTouchUpInside]; // 为toolBar中的第2个UIToolbarTextButton(下一个button)加入点击事件
//[[_toolBar.subviews objectAtIndex:2] addTarget:self action:@selector(nextKeyboard:) forControlEvents:UIControlEventTouchUpInside]; // 为toolBar中的最后一个UIToolbarTextButton(完毕button)加入点击事件
[[_toolBar.subviews lastObject] addTarget:self action:@selector(exitKeyboard:) forControlEvents:UIControlEventTouchUpInside];
}
// toolBar里面,点击上一个button
- (void)previousKeyboard:(UIButton *)sender
{
NSLog(@"点击了上一个button,要激活上一个输入框");
}
// toolBar里面,点击下一个button
- (void)nextKeyboard:(UIButton *)sender
{
NSLog(@"点击了下一个button,要激活下一个输入框");
} // 退出键盘
- (IBAction)exitKeyboard:(UIButton *)sender {
// 方式1: self.view内部全部的文本框(包含子孙控件...)都退出第一响应者
[self.view endEditing:YES];
return; // 方式2:
// 遍历uiview里面全部的控件 ,resignFirstResponder
/*
for (int i=0; i<self.view.subviews.count; i++) {
[self.view.subviews[i] resignFirstResponder];
}
*/
NSArray *array = self.view.subviews;
for (UIView *obj in array) {
if ([obj isKindOfClass:[UITextField class]]) {
[obj resignFirstResponder];
}
} // 方式3:
// 在self.view的最开头,铺一个全屏的透明的button,连线,仅仅要在屏幕空白区域点击后,就能够调用上面的方式1,退出键盘 } @end

KeyToolBar.xib

版权声明:本文博主原创文章,博客,未经同意不得转载。

最新文章

  1. SQL中select与set的区别-转载
  2. Redis——学习之路二(初识redis服务器命令)
  3. .net学习笔记----HttpRequest类
  4. office 2013 产品秘钥
  5. LintCode &quot;Binary Representation&quot;
  6. 字符串(AC自动机):HDU 5129 Yong Zheng&#39;s Death
  7. db2 identity列重置,reset/restart
  8. NET Core+Code First+Docker
  9. Hdu 3341 Lost&amp;#39;s revenge (ac+自己主动机dp+hash)
  10. [LeetCode] 2 Keys Keyboard 两键的键盘
  11. 在Vue项目中 选择图片并预览
  12. linux中.nfsxxxx引起的文件无法删除
  13. MDK错误 Error: L6218E: Undefined symbol SystemInit (referred from startup_stm32f10x_hd.o). 解决方法
  14. leetcode python 032 识别最长合法括号
  15. [leetcode]75. Sort Colors三色排序
  16. Codeforces Round #323
  17. httpd.conf文件格式解析
  18. 死无对证:tomcat7 + 中文cookie + goLang
  19. FDQuery多表更新生成sql语句的问题
  20. ExtJS+SpringMVC文件上传与下载

热门文章

  1. Lucene.Net 2.3.1开发介绍 —— 三、索引(五)
  2. jquery ajax验证用户名是否存在(后台spring mvc)
  3. poj 3623 Best Cow Line, Gold
  4. ANR的一个实例分析
  5. xp下Oracle数据库导入SQLServer数据库数据
  6. Core Data 和 sqlite3的性能对比【图】3gs,iPhone4,4s,5的性能测试。
  7. Codechef Not a Triangle题解
  8. Mysql 导入导出数据结构及数据
  9. latex命令替换之\newcommand
  10. NET实现的DDD、CQRS与微服务架构