通知中心——键盘的通知

在我们使用的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 ()<UITextFieldDelegate>
{
    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

效果如下:

   

  

 

posted @ 2016-02-13 21:38  〤twinkle゛新  阅读(387)  评论(0编辑  收藏  举报