博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通知中心——键盘的通知
阅读量:7002 次
发布时间:2019-06-27

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

在我们使用的App中,大家都可能见过当编辑的时候键盘会显示出来,当编辑完成的时候点击键盘上面的return,键盘会回收到视图的下面。

这是怎么做到的呢?——通知中心:NSNotificationCenter

* 通知中心:NSNotificationCenter 分为发送和观察者

   俗意:农村大喇叭发送广播 在大喇叭广播之前要有村民(对象) 大喇叭发送广播 村民接收到广播

   大喇叭发送广播:

   - (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

   <每一条通知 都是通过通知名字来区分是哪条通知> <发送通知 需要给观察者 一个内容 可以使用object(id) userInfo(字典)>

  村民(观察者)接收广播:

   - (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject;

   <接收到广播 观察者可以去做某件事> <观察者接收到通知 执行方法的时候 同时会得到一个通知(NSNotificationCenter)->通知包含:名字 object userInfo>

   村民死了 不再接受广播(移除观察者):

   - (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject;

 * 通知可以有一个发送者 可以有多个接收者(观察者)

 * 通知中心实例化对象->[NSNotificationCenter defaultCenter](通知中心方法、单例)

以上就是对通知的介绍,具体怎么操作看代码:

 在AppDelegate.m里面导入ViewController.h文件,创建带有导航栏的试图控制器

#import "AppDelegate.h"#import "ViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];    [self.window makeKeyAndVisible];    return YES;}@end

在ViewController.m文件

#import "ViewController.h"@interface ViewController ()
{ UITextField *textField1;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000]; /* 键盘通知 UIKeyboardWillShowNotification UIKeyboardDidShowNotification UIKeyboardWillHideNotification UIKeyboardDidHideNotification */ //输入框一直在键盘的上面浮着 如果没有键盘的话 会跟键盘一块到屏幕的最底部 //键盘在显示、消失的时候都会发出两个通知(即将、已经) textField1 = [[UITextField alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-44, self.view.frame.size.width, 44)]; textField1.borderStyle = UITextBorderStyleRoundedRect; textField1.delegate = self; [self.view addSubview:textField1]; //添加观察者 键盘即将显示的时候 通知名字UIKeyboardWillShowNotification [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardAction:) name:UIKeyboardWillShowNotification object:nil]; //即将消失 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardAction:) name:UIKeyboardWillHideNotification object:nil];}- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; }- (void)keyboardAction:(NSNotification *)not{ NSLog(@"%@",not.userInfo); NSDictionary *dic = not.userInfo; NSLog(@"%@",dic[UIKeyboardFrameEndUserInfoKey]); //frame是CGRect(结构体) //字典取出来的内容是id类型->CGRect(结构体) CGRect frame = [dic[UIKeyboardFrameEndUserInfoKey]CGRectValue]; //获得键盘起始点Y轴的高度 CGFloat y = CGRectGetMinY(frame); textField1.frame = CGRectMake(0, y-44, self.view.frame.size.width, 44);}@end

效果如下:

   

  

 

转载于:https://www.cnblogs.com/chengy134/p/5388909.html

你可能感兴趣的文章
开源项目学习方法
查看>>
block的使用
查看>>
使用Toolbar自定义布局的时候左边右边总有一点空间无法使用
查看>>
Photoshop 常用快捷键
查看>>
外观模式
查看>>
Extjs 4 grid修改某一行style
查看>>
background-position设置无效问题解决
查看>>
对称加密算法-DES
查看>>
Android BroadcastReceiver
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
jar包版本冲突问题
查看>>
物联网世界常见传输方式简介(思维导图)
查看>>
KSM导致的警告“ ksmtuned .... read-only system ” 的一些说明
查看>>
Objective C中数组排序的三种方法
查看>>
dedecms验证自定义表单不为空
查看>>
用户测评 | EDAS Serverless 上手体验
查看>>
mysql导出xls的格式
查看>>
开发者招聘节 | 2019阿里巴巴技术面试题分享(陆续放出)
查看>>
Linux 虚拟化实践之KVM
查看>>