*****HMViewController.m

  1. #import "HMViewController.h"
  2. #import "HMKeyboardTool.h"
  3.  
  4. @interface HMViewController ()<HMKeyboardToolDelegate>{
  5.  
  6. NSArray *_fields;//存储所有的textField
  7. }
  8. @property (weak, nonatomic) IBOutlet UITextField *birthdayField;
  9. @property (weak, nonatomic) IBOutlet UIView *inputContainer;//输入框容器view
  10.  
  11. @end
  12.  
  13. @implementation HMViewController
  14.  
  15. - (void)viewDidLoad
  16. {
  17. [super viewDidLoad];
  18. // Do any additional setup after loading the view, typically from a nib.
  19.  
  20. //1.初始化自定议键盘
  21. [self setupCustomKeyboard];
  22.  
  23. //2.设置每一个textfield的键盘工具view(inputAccessoryView)
  24.  
  25. [self setupKeyboardTool];
  26.  
  27. //3.监听键盘的事件
  28. [self setupKeyoardNotification];
  29.  
  30. NSLog(@"%@",_fields);
  31. }
  32.  
  33. //1.初始化自定议键盘
  34. -(void)setupCustomKeyboard{
  35. UIDatePicker *datePicker = [[UIDatePicker alloc] init];
  36. datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
  37. datePicker.datePickerMode = UIDatePickerModeDate;
  38.  
  39. self.birthdayField.inputView = datePicker;
  40. }
  41.  
  42. //设置每一个textfield的键盘工具view(inputAccessoryView)
  43. -(void)setupKeyboardTool{
  44.  
  45. //创建工具栏
  46. HMKeyboardTool *tool = [HMKeyboardTool keyboardTool];
  47.  
  48. //设置代理
  49. tool.delgate = self;
  50.  
  51. //1.获取输入框窗口所有子控件
  52. NSArray *views = self.inputContainer.subviews;
  53.  
  54. //创建一个数据存储textfield
  55. NSMutableArray *fieldsM = [NSMutableArray array];
  56.  
  57. //2.遍历
  58. for (UIView *child in views) {
  59. //如果子控制是UITextField的时候,设置inputAccessoryView
  60. if ([child isKindOfClass:[UITextField class]]) {
  61. UITextField *tf = (UITextField *)child;
  62. tf.inputAccessoryView = tool;
  63. //把textfield添加到数组
  64. [fieldsM addObject:tf];
  65. }
  66. }
  67.  
  68. _fields = fieldsM;
  69.  
  70. }
  71.  
  72. -(void)dealloc{
  73. [[NSNotificationCenter defaultCenter] removeObserver:self];
  74. }
  75.  
  76. //3.监听键盘的事件
  77. -(void)setupKeyoardNotification{
  78.  
  79. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
  80.  
  81. }
  82.  
  83. //键盘的frame的变化
  84.  
  85. -(void)kbFrameChange:(NSNotification *)nofifi{
  86. //NSLog(@"%@",nofifi);
  87.  
  88. //获取键盘改变化的y值
  89. //这键盘结束时的frm
  90. CGRect kbEndFrm =[nofifi.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  91.  
  92. //键盘结束时的y
  93. CGFloat kbEndY = kbEndFrm.origin.y;
  94.  
  95. //获取当前的响应者
  96. int currentIndex = [self getCurrentResponderIndex];
  97. UITextField *currentTf = _fields[currentIndex];
  98. CGFloat tfMaxY = CGRectGetMaxY(currentTf.frame) + self.inputContainer.frame.origin.y;
  99.  
  100. NSLog(@"%f %f",kbEndY,tfMaxY);
  101. //改变控制器view的transform
  102.  
  103. //如果textfield的最大值在于键盘的y坐,才要往上移
  104. if (tfMaxY > kbEndY) {
  105. [UIView animateWithDuration:0.25 animations:^{
  106. self.view.transform = CGAffineTransformMakeTranslation(, kbEndY - tfMaxY);
  107. }];
  108. }else{
  109. [UIView animateWithDuration:0.25 animations:^{
  110. self.view.transform = CGAffineTransformIdentity;
  111. }];
  112. }
  113.  
  114. }
  115.  
  116. #pragma mark 键盘工具条的代理
  117. -(void)keyboardTool:(HMKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType{
  118.  
  119. //获取当前的响应者的索引
  120. int currentIndex = [self getCurrentResponderIndex];
  121. NSLog(@"当前的响应者 %d",currentIndex);
  122.  
  123. if (itemType == KeyboardItemTypePrevious) {
  124. NSLog(@"上一个");
  125. //让上一个field成功响应者
  126. [self showProviousField:currentIndex];
  127. }else if(itemType == KeyboardItemTypeNext){
  128. NSLog(@"下一个");
  129.  
  130. //让下一个field成功响应者
  131. [self showNextField:currentIndex];
  132. }else{
  133. NSLog(@"完成");
  134. [self touchesBegan:nil withEvent:nil];
  135. }
  136. }
  137.  
  138. //获取当前textfield的响应者索引
  139. //如果返回-1代理没有找响应者
  140. -(int)getCurrentResponderIndex{
  141. //遍历所有的textfield获取响应者
  142. for (UITextField *tf in _fields) {
  143. if (tf.isFirstResponder) {
  144. return [_fields indexOfObject:tf];
  145. }
  146. }
  147.  
  148. return -;
  149. }
  150.  
  151. //让上一个field成功响应者
  152. -(void)showProviousField:(int)currentIndex{
  153. int proviousIndex = currentIndex - ;
  154.  
  155. if (proviousIndex > ) {
  156. UITextField *proviousTf = [_fields objectAtIndex:proviousIndex];
  157. [proviousTf becomeFirstResponder];
  158. }
  159.  
  160. }
  161.  
  162. //让下一个field成功响应者
  163. -(void)showNextField:(int)currentIndex{
  164. int nextIndex = currentIndex + ;
  165.  
  166. //下一个索引不能超过_fields数组的个数
  167. if (nextIndex < _fields.count) {
  168. //让当前响应者分发去
  169. UITextField *currentTf = [_fields objectAtIndex:currentIndex];
  170. [currentTf resignFirstResponder];
  171.  
  172. UITextField *nextTf = [_fields objectAtIndex:nextIndex];
  173. [nextTf becomeFirstResponder];
  174. }
  175.  
  176. }
  177.  
  178. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  179. [self.view endEditing:YES];
  180.  
  181. [UIView animateWithDuration:0.25 animations:^{
  182. self.view.transform = CGAffineTransformIdentity;
  183. }];
  184. }
  185. @end

****自定义view,HMKeyboardTool.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. typedef enum {
  4. KeyboardItemTypePrevious,//上一个
  5. KeyboardItemTypeNext,//下一个
  6. KeyboardItemTypeDone//完成
  7. }KeyboardItemType;
  8.  
  9. @class HMKeyboardTool;
  10.  
  11. @protocol HMKeyboardToolDelegate <NSObject>
  12.  
  13. -(void)keyboardTool:(HMKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType;
  14.  
  15. @end
  16.  
  17. @interface HMKeyboardTool : UIView
  18.  
  19. //添加代理
  20. @property(nonatomic,weak)id<HMKeyboardToolDelegate> delgate;
  21.  
  22. +(instancetype)keyboardTool;
  23.  
  24. @end

****自定义HMKeyboardTool.m

  1. #import "HMKeyboardTool.h"
  2.  
  3. @interface HMKeyboardTool()
  4.  
  5. - (IBAction)previous:(id)sender;
  6.  
  7. - (IBAction)next:(id)sender;
  8. - (IBAction)done:(id)sender;
  9.  
  10. @end
  11.  
  12. @implementation HMKeyboardTool
  13.  
  14. +(instancetype)keyboardTool{
  15. return [[[NSBundle mainBundle] loadNibNamed:@"HMKeyboardTool" owner:nil options:nil] lastObject];
  16. }
  17.  
  18. //上一个
  19. - (IBAction)previous:(id)sender {
  20.  
  21. //判断代理有没有实现相应的方法
  22. if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
  23. [self.delgate keyboardTool:self didClickItemType:KeyboardItemTypePrevious];
  24. }
  25. }
  26.  
  27. //下一个
  28. - (IBAction)next:(id)sender {
  29. //判断代理有没有实现相应的方法
  30. if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
  31. [self.delgate keyboardTool:self didClickItemType:KeyboardItemTypeNext];
  32. }
  33.  
  34. }
  35.  
  36. //完成
  37. - (IBAction)done:(id)sender {
  38.  
  39. //判断代理有没有实现相应的方法
  40. if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
  41. [self.delgate keyboardTool:self didClickItemType:KeyboardItemTypeDone];
  42. }
  43. }
  44. @end

IOS第12天(4,作业弹出键盘上加上(xib)view的处理,时间选择,代理模式使用,键盘的监听 )的更多相关文章

  1. ios手机弹出层上表单的操作,收起键盘焦点错乱的问题

    今天遇到了ios手机下 弹出层上form表单 当收起键盘后,焦点错乱,无法再操作的问题 解决办法 function device() { const u = navigator.userAgent; ...

  2. 【土旦】vue 解决ios H5底部输入框 获取焦点时弹出虚拟键盘挡住输入框 以及监听键盘收起事件

    问题描述 im聊天H5页面,在iOS系统下,inpu获取焦点弹出系统虚拟键盘时,会出现挡住input的情况,十分影响用户体验. bug图 解决方法: html: <input type=&quo ...

  3. QMessageBox 弹出框上的按钮设置为中文

    Qt 默认的弹出框上的按钮式英文,虽然也知道是什么意思,但终究不如中文看着顺眼. QMessageBox box(QMessageBox::Warning,"标题","弹 ...

  4. 练习PopupWindow弹出框之实现界面加载的时候显示弹出框到指定的view下面--两种延迟方法

    今天在练习PopupWindow弹出框的时候,打算在界面加载的时候将弹出框展现出来并显示在指定的view下面. 初步方法是直接在OnResume方法里面直接执行showPopupWindows方法. ...

  5. iOS bug 之 H5 页面没有弹出提示框

    描述:在安卓上有提示框,但是在iOS上没有提示框. step 1: 失误,是我没有在正确的位置设置网址. step 2: 修改之后,测试页能弹出提示框,但是正式的页面没有提示框. step 3: 我输 ...

  6. iOS如何禁用长按页面弹出菜单

    iOS如何禁止用户长按页面导致弹出菜单? 给元素设置样式: -webkit-touch-callout:none; 补充:同样适用于图片如果想禁止用户保存或者复制等

  7. iOS开发——UI篇&下拉弹出列表选择项效果

    下拉弹出列表选择项效果 右边菜单中的按键,点击弹出一个列表可选择,选择其中一个,响应相应的事件并把文字显示在右边的菜单上:弹出下拉效果使用LMDropdownView插件,可以用POD进行加载pod  ...

  8. iOS开发 编辑框被系统弹出的软键盘遮挡问题

    我们在开发注冊界面的时候,最后几个注冊条件经常easy被系统弹出的键盘遮挡,例如以下图: 能够看见,邮箱条件被遮挡掉了,怎么解决呢?我是通过UITextField的代理加计算偏移量: - (void) ...

  9. iOS开发- 自己主动消失的弹出框

    - (void)timerFireMethod:(NSTimer*)theTimer//弹出框 { UIAlertView *promptAlert = (UIAlertView*)[theTimer ...

随机推荐

  1. Editthiscookie

    Editthiscookie,联调,.s环境加cookie才能访问.laravel

  2. testng.xml文件结构组成及节点属性说明

    TestNG的DTD检查文件:http://testng.org/testng-1.0.dtd.PHP 更多testng配置及说明,请移步http://testdoc.org/docmaster?pi ...

  3. Dapper ORM 用法—Net下无敌的ORM(转)

    假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载Dapper的优势:1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编译后 ...

  4. tomcat与HTML命令提示符

    在tomcatwebapps目录下建立一个新文件夹 命名为my 把第一个学习的HTML文件放到my文件夹内 通过tomcat服务器远程访问该网页 把localhost换成自己的IP地址 先查看自己的I ...

  5. The 2015 China Collegiate Programming Contest E. Ba Gua Zhen hdu 5544

    Ba Gua Zhen Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  6. 【BZOJ1984】月下“毛景树” 树链剖分+线段树

    [BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...

  7. Spring MVC Rest 支持CORS

    新建cors filter文件, package cn.ac.iscas.pebble.ufe.tools; import java.io.IOException; import javax.serv ...

  8. HDU 3333 & 离线+线段树

    题意: 统计一段区间内不同数字之和.如1 1 2 3 1 统计2---5即1+2+3. SOL: 很少打过离线的题目...这种可离线可在线的题不管怎么样一般都是强行在线... 考虑这题,此前做过一个类 ...

  9. ACM 精挑细选

    精 挑 细 选 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 小王是公司的仓库管理员,一天,他接到了这样一个任务:从仓库中找出一根钢管.这听起来不算什么,但是这根钢 ...

  10. 定时器的fireDate指的是触发时间

    1.定时器开启后,会在经过设定的时间间隔后才会执行第一次定时操作.而不是立马开启. NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: ...