博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NSNotificationCenter+ UITapGestureRecognizer
阅读量:5043 次
发布时间:2019-06-12

本文共 4580 字,大约阅读时间需要 15 分钟。

这里涉及比较多,包括手势,通知中心等,具体上代码
#import "RegistViewController.h"#import "DetailViewController.h"@interface RegistViewController ()
- (IBAction)goDetail:(id)sender;@property (weak, nonatomic) IBOutlet UITextField *nameTextField;@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;@property (weak, nonatomic) IBOutlet UIButton *goDetailButton;@end@implementation RegistViewController{ CGRect buttonOldRect; UITapGestureRecognizer *tapGR;//添加单击手势}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; /* 数据传递 1、首先,数据已经存在了! 2、谁需要,就声明几个指针。 3、在实例化2那个对象的时候,将声明的几个指针指向正确是数据。 4、至此,数据传递完成,可以使用了。 */ //将自己加入通知中心 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyBoradShow:) name:UIKeyboardWillShowNotification object:Nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyBoradHide:) name:UIKeyboardWillHideNotification object:Nil]; buttonOldRect=self.goDetailButton.frame; //实例化单击手势 tapGR=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)]; self.nameTextField.clearButtonMode=UITextFieldViewModeWhileEditing; self.passwordTextField.secureTextEntry=YES; self.nameTextField.delegate=self; [self.nameTextField becomeFirstResponder];//成为第一响应者,处于可编辑状态}#pragma mark--#pragma mark 自定义函数//单击手势的响应函数-(void)onTap:(id)sender{ //将输入框取消第一响应者,键盘就睡自动收回 [self.nameTextField resignFirstResponder]; [self.passwordTextField resignFirstResponder];}//收到键盘将要弹起的通知后,调用的函数-(void)onKeyBoradShow:(id)sender{ [self.view addGestureRecognizer:tapGR];// NSLog(@"%@",sender); //将sender转化成notice类型 NSNotification *notice=sender; //获取通知里面的userinfo NSDictionary *userInfo = notice.userInfo; //在userinfo中获取键盘最后的状态rect id keyBoardRectNotice = userInfo[UIKeyboardFrameEndUserInfoKey]; //将上面获得的rect对象,提取出cgrect CGRect keyBoardRect; [keyBoardRectNotice getValue:&keyBoardRect]; //用获得的cgrect来设置button的frame; CGRect buttonNewFrame=buttonOldRect; //原来的按键frame减去键盘的高度,生成新的frame buttonNewFrame.origin.y-=keyBoardRect.size.height; [UIView animateWithDuration:0.2 animations:^{ self.goDetailButton.frame=buttonNewFrame; }]; }//收到键盘将要收回的通知后,调用的函数-(void)onKeyBoradHide:(id)sender{ [self.view removeGestureRecognizer:tapGR]; [UIView animateWithDuration:0.3 animations:^{ self.goDetailButton.frame=buttonOldRect; }];}//按键函数- (IBAction)goDetail:(id)sender { NSString *name=self.nameTextField.text; NSString *password=self.passwordTextField.text; //生成数据模型 UserModel *model=[[UserModel alloc]init]; model.name=name; model.pass=password; DetailViewController *detailVC=[[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:Nil]; //将数据模型传入 detailVC.model=model; [self presentViewController:detailVC animated:YES completion:^{ }]; }#pragma mark--#pragma mark textfiled代理方法- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ NSLog(@"textFiled 是否可以开始编辑"); return YES;}- (void)textFieldDidBeginEditing:(UITextField *)textField{ NSLog(@"textFiled 开始编辑");}// became first responder- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ NSLog(@"textFiled 是否可以结束编辑"); return YES;}- (void)textFieldDidEndEditing:(UITextField *)textField{ NSLog(@"textFiled 结束编辑");}- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSLog(@"textFiled 是否可以改变某个区域字符串"); NSLog(@"%d-%d,%@",range.location,range.length,string); if (textField==self.nameTextField) { if (range.location>=8) { return NO; }else{ if (textField.text.length>=8) { if (range.length==0) { return NO; }else{ return YES; } } } } return YES;}- (BOOL)textFieldShouldClear:(UITextField *)textField{ NSLog(@"textFiled 是否清空"); return YES;}- (BOOL)textFieldShouldReturn:(UITextField *)textField{ NSLog(@"textFiled 键盘的return键被点击"); [textField resignFirstResponder]; NSLog(@"开始搜索!!!"); return YES;}//销毁函数,用于移除通知-(void)dealloc{ //在销毁的时候将自己在通知中心中移除 [[NSNotificationCenter defaultCenter]removeObserver:self];}

转载于:https://www.cnblogs.com/zjszyms/p/4119455.html

你可能感兴趣的文章
photoplus
查看>>
Python 拓展之推导式
查看>>
[Leetcode] DP-- 474. Ones and Zeroes
查看>>
80X86寄存器详解<转载>
查看>>
c# aop讲解
查看>>
iterable与iterator
查看>>
返回顶部(动画)
查看>>
webpack+react+antd 单页面应用实例
查看>>
Confluence 6 SQL Server 数据库驱动修改
查看>>
Confluence 6 通过 SSL 或 HTTPS 运行 - 备注和问题解决
查看>>
【47.76%】【Round #380B】Spotlights
查看>>
Git(使用码云)
查看>>
分享Java web 开发必游之路
查看>>
IIS初始化(预加载),解决第一次访问慢,程序池被回收问题(转载)
查看>>
Bean的Scope
查看>>
【BZOJ】3142: [Hnoi2013]数列
查看>>
http初探
查看>>
elasticsearch的安装
查看>>
__next__()
查看>>
爬取:中国大学排名
查看>>