IOS第12天(4,作业弹出键盘上加上(xib)view的处理,时间选择,代理模式使用,键盘的监听 )
*****HMViewController.m
- #import "HMViewController.h"
- #import "HMKeyboardTool.h"
- @interface HMViewController ()<HMKeyboardToolDelegate>{
- NSArray *_fields;//存储所有的textField
- }
- @property (weak, nonatomic) IBOutlet UITextField *birthdayField;
- @property (weak, nonatomic) IBOutlet UIView *inputContainer;//输入框容器view
- @end
- @implementation HMViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- //1.初始化自定议键盘
- [self setupCustomKeyboard];
- //2.设置每一个textfield的键盘工具view(inputAccessoryView)
- [self setupKeyboardTool];
- //3.监听键盘的事件
- [self setupKeyoardNotification];
- NSLog(@"%@",_fields);
- }
- //1.初始化自定议键盘
- -(void)setupCustomKeyboard{
- UIDatePicker *datePicker = [[UIDatePicker alloc] init];
- datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
- datePicker.datePickerMode = UIDatePickerModeDate;
- self.birthdayField.inputView = datePicker;
- }
- //设置每一个textfield的键盘工具view(inputAccessoryView)
- -(void)setupKeyboardTool{
- //创建工具栏
- HMKeyboardTool *tool = [HMKeyboardTool keyboardTool];
- //设置代理
- tool.delgate = self;
- //1.获取输入框窗口所有子控件
- NSArray *views = self.inputContainer.subviews;
- //创建一个数据存储textfield
- NSMutableArray *fieldsM = [NSMutableArray array];
- //2.遍历
- for (UIView *child in views) {
- //如果子控制是UITextField的时候,设置inputAccessoryView
- if ([child isKindOfClass:[UITextField class]]) {
- UITextField *tf = (UITextField *)child;
- tf.inputAccessoryView = tool;
- //把textfield添加到数组
- [fieldsM addObject:tf];
- }
- }
- _fields = fieldsM;
- }
- -(void)dealloc{
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- //3.监听键盘的事件
- -(void)setupKeyoardNotification{
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
- }
- //键盘的frame的变化
- -(void)kbFrameChange:(NSNotification *)nofifi{
- //NSLog(@"%@",nofifi);
- //获取键盘改变化的y值
- //这键盘结束时的frm
- CGRect kbEndFrm =[nofifi.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
- //键盘结束时的y
- CGFloat kbEndY = kbEndFrm.origin.y;
- //获取当前的响应者
- int currentIndex = [self getCurrentResponderIndex];
- UITextField *currentTf = _fields[currentIndex];
- CGFloat tfMaxY = CGRectGetMaxY(currentTf.frame) + self.inputContainer.frame.origin.y;
- NSLog(@"%f %f",kbEndY,tfMaxY);
- //改变控制器view的transform
- //如果textfield的最大值在于键盘的y坐,才要往上移
- if (tfMaxY > kbEndY) {
- [UIView animateWithDuration:0.25 animations:^{
- self.view.transform = CGAffineTransformMakeTranslation(, kbEndY - tfMaxY);
- }];
- }else{
- [UIView animateWithDuration:0.25 animations:^{
- self.view.transform = CGAffineTransformIdentity;
- }];
- }
- }
- #pragma mark 键盘工具条的代理
- -(void)keyboardTool:(HMKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType{
- //获取当前的响应者的索引
- int currentIndex = [self getCurrentResponderIndex];
- NSLog(@"当前的响应者 %d",currentIndex);
- if (itemType == KeyboardItemTypePrevious) {
- NSLog(@"上一个");
- //让上一个field成功响应者
- [self showProviousField:currentIndex];
- }else if(itemType == KeyboardItemTypeNext){
- NSLog(@"下一个");
- //让下一个field成功响应者
- [self showNextField:currentIndex];
- }else{
- NSLog(@"完成");
- [self touchesBegan:nil withEvent:nil];
- }
- }
- //获取当前textfield的响应者索引
- //如果返回-1代理没有找响应者
- -(int)getCurrentResponderIndex{
- //遍历所有的textfield获取响应者
- for (UITextField *tf in _fields) {
- if (tf.isFirstResponder) {
- return [_fields indexOfObject:tf];
- }
- }
- return -;
- }
- //让上一个field成功响应者
- -(void)showProviousField:(int)currentIndex{
- int proviousIndex = currentIndex - ;
- if (proviousIndex > ) {
- UITextField *proviousTf = [_fields objectAtIndex:proviousIndex];
- [proviousTf becomeFirstResponder];
- }
- }
- //让下一个field成功响应者
- -(void)showNextField:(int)currentIndex{
- int nextIndex = currentIndex + ;
- //下一个索引不能超过_fields数组的个数
- if (nextIndex < _fields.count) {
- //让当前响应者分发去
- UITextField *currentTf = [_fields objectAtIndex:currentIndex];
- [currentTf resignFirstResponder];
- UITextField *nextTf = [_fields objectAtIndex:nextIndex];
- [nextTf becomeFirstResponder];
- }
- }
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
- [self.view endEditing:YES];
- [UIView animateWithDuration:0.25 animations:^{
- self.view.transform = CGAffineTransformIdentity;
- }];
- }
- @end
****自定义view,HMKeyboardTool.h
- #import <UIKit/UIKit.h>
- typedef enum {
- KeyboardItemTypePrevious,//上一个
- KeyboardItemTypeNext,//下一个
- KeyboardItemTypeDone//完成
- }KeyboardItemType;
- @class HMKeyboardTool;
- @protocol HMKeyboardToolDelegate <NSObject>
- -(void)keyboardTool:(HMKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType;
- @end
- @interface HMKeyboardTool : UIView
- //添加代理
- @property(nonatomic,weak)id<HMKeyboardToolDelegate> delgate;
- +(instancetype)keyboardTool;
- @end
****自定义HMKeyboardTool.m
- #import "HMKeyboardTool.h"
- @interface HMKeyboardTool()
- - (IBAction)previous:(id)sender;
- - (IBAction)next:(id)sender;
- - (IBAction)done:(id)sender;
- @end
- @implementation HMKeyboardTool
- +(instancetype)keyboardTool{
- return [[[NSBundle mainBundle] loadNibNamed:@"HMKeyboardTool" owner:nil options:nil] lastObject];
- }
- //上一个
- - (IBAction)previous:(id)sender {
- //判断代理有没有实现相应的方法
- if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
- [self.delgate keyboardTool:self didClickItemType:KeyboardItemTypePrevious];
- }
- }
- //下一个
- - (IBAction)next:(id)sender {
- //判断代理有没有实现相应的方法
- if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
- [self.delgate keyboardTool:self didClickItemType:KeyboardItemTypeNext];
- }
- }
- //完成
- - (IBAction)done:(id)sender {
- //判断代理有没有实现相应的方法
- if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
- [self.delgate keyboardTool:self didClickItemType:KeyboardItemTypeDone];
- }
- }
- @end
IOS第12天(4,作业弹出键盘上加上(xib)view的处理,时间选择,代理模式使用,键盘的监听 )的更多相关文章
- ios手机弹出层上表单的操作,收起键盘焦点错乱的问题
今天遇到了ios手机下 弹出层上form表单 当收起键盘后,焦点错乱,无法再操作的问题 解决办法 function device() { const u = navigator.userAgent; ...
- 【土旦】vue 解决ios H5底部输入框 获取焦点时弹出虚拟键盘挡住输入框 以及监听键盘收起事件
问题描述 im聊天H5页面,在iOS系统下,inpu获取焦点弹出系统虚拟键盘时,会出现挡住input的情况,十分影响用户体验. bug图 解决方法: html: <input type=&quo ...
- QMessageBox 弹出框上的按钮设置为中文
Qt 默认的弹出框上的按钮式英文,虽然也知道是什么意思,但终究不如中文看着顺眼. QMessageBox box(QMessageBox::Warning,"标题","弹 ...
- 练习PopupWindow弹出框之实现界面加载的时候显示弹出框到指定的view下面--两种延迟方法
今天在练习PopupWindow弹出框的时候,打算在界面加载的时候将弹出框展现出来并显示在指定的view下面. 初步方法是直接在OnResume方法里面直接执行showPopupWindows方法. ...
- iOS bug 之 H5 页面没有弹出提示框
描述:在安卓上有提示框,但是在iOS上没有提示框. step 1: 失误,是我没有在正确的位置设置网址. step 2: 修改之后,测试页能弹出提示框,但是正式的页面没有提示框. step 3: 我输 ...
- iOS如何禁用长按页面弹出菜单
iOS如何禁止用户长按页面导致弹出菜单? 给元素设置样式: -webkit-touch-callout:none; 补充:同样适用于图片如果想禁止用户保存或者复制等
- iOS开发——UI篇&下拉弹出列表选择项效果
下拉弹出列表选择项效果 右边菜单中的按键,点击弹出一个列表可选择,选择其中一个,响应相应的事件并把文字显示在右边的菜单上:弹出下拉效果使用LMDropdownView插件,可以用POD进行加载pod ...
- iOS开发 编辑框被系统弹出的软键盘遮挡问题
我们在开发注冊界面的时候,最后几个注冊条件经常easy被系统弹出的键盘遮挡,例如以下图: 能够看见,邮箱条件被遮挡掉了,怎么解决呢?我是通过UITextField的代理加计算偏移量: - (void) ...
- iOS开发- 自己主动消失的弹出框
- (void)timerFireMethod:(NSTimer*)theTimer//弹出框 { UIAlertView *promptAlert = (UIAlertView*)[theTimer ...
随机推荐
- Editthiscookie
Editthiscookie,联调,.s环境加cookie才能访问.laravel
- testng.xml文件结构组成及节点属性说明
TestNG的DTD检查文件:http://testng.org/testng-1.0.dtd.PHP 更多testng配置及说明,请移步http://testdoc.org/docmaster?pi ...
- Dapper ORM 用法—Net下无敌的ORM(转)
假如你喜欢原生的Sql语句,又喜欢ORM的简单,那你一定会喜欢上Dapper这款ROM.点击下载Dapper的优势:1,Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,编译后 ...
- tomcat与HTML命令提示符
在tomcatwebapps目录下建立一个新文件夹 命名为my 把第一个学习的HTML文件放到my文件夹内 通过tomcat服务器远程访问该网页 把localhost换成自己的IP地址 先查看自己的I ...
- 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 ...
- 【BZOJ1984】月下“毛景树” 树链剖分+线段树
[BZOJ1984]月下"毛景树" Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校 ...
- Spring MVC Rest 支持CORS
新建cors filter文件, package cn.ac.iscas.pebble.ufe.tools; import java.io.IOException; import javax.serv ...
- HDU 3333 & 离线+线段树
题意: 统计一段区间内不同数字之和.如1 1 2 3 1 统计2---5即1+2+3. SOL: 很少打过离线的题目...这种可离线可在线的题不管怎么样一般都是强行在线... 考虑这题,此前做过一个类 ...
- ACM 精挑细选
精 挑 细 选 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 小王是公司的仓库管理员,一天,他接到了这样一个任务:从仓库中找出一根钢管.这听起来不算什么,但是这根钢 ...
- 定时器的fireDate指的是触发时间
1.定时器开启后,会在经过设定的时间间隔后才会执行第一次定时操作.而不是立马开启. NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: ...