zoukankan      html  css  js  c++  java
  • ios 键盘通知

    通知 (Notification): 通过 观察者模式 实现

    作用: 完成对象之间的消息传递

    核心理念

    • 通知的发送方不知道接收方是谁,只需要将通知发给通知中即可
    • 通知的接收方不知道发送方是谁,只需要提前向通知中心订阅某个通知即可
    • 一旦通知的发送方真的发出了通知,则有通知中心负责将这个通知推送给所有订阅了该通知的接收方

    自定义通知
    接收方和发送方都是我们自己写的

    系统提供的通知

    • 键盘通知
      每当键盘弹起时系统会自动发出名字叫
      > UIKeyboardWillShowNotification
      > UIKeyboardDidShowNotification
      每当键盘收起时系统会自动发出名字叫
      >UIKeyboardWillHideNotification
      >UIKeyboardDidHideNotification

    键盘通知:

    //键盘通知
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        [self.view endEditing:YES];
    }
    
    -(void)viewWillAppear:(BOOL)animated {
        //向通知中心注册 接收 键盘将要弹起的通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(openKeyBoard:) name:UIKeyboardWillShowNotification object:nil];
    
        //向通知中心注册 接收 键盘将要收起的通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(closeKeyBoard:) name:UIKeyboardWillHideNotification object:nil];
    }
    
    //已经不再显示
    -(void)viewDidDisappear:(BOOL)animated {
        //removeObserver 从通知中心 移除 之前监听的通知
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       
    }
    -(void)openKeyBoard:(NSNotification*)notification {
        NSLog(@"键盘将要弹起");
    }
    -(void)closeKeyBoard:(NSNotification*)notification {
        NSLog(@"键盘将要收起");
    }
    

    自定义通知

    //获取通知并拟定通知内容
    @implementation CCTV
    -(void)send {
    //    1.获取通知中心
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
        //2.发通知(  name : 通知的名字,    object : 谁发的通知,  userInfo : 通知的内容)
        [center postNotificationName:@"cctv2" object:self userInfo:@{@"time":@"6:20", @"name":@"明天来新老师"}];
    }
    @end
    
    //订阅通知
    @implementation MyTv
    -(instancetype)init {
        if (self = [super init]) {
            //向通知中心 订阅名字叫 cctv2 的通知
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(play:) name:@"cctv2" object:nil];
        }
        return self;
    }
    
    //接收通知调用的方法
    -(void)play:(NSNotification*)notification {
        NSDictionary *dic = notification.userInfo;
        NSLog(@"中央2台%@点,播放%@综艺节目",dic[@"time"],dic[@"name"]);
    }
    @end
    
    //初始化
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.cctv = [[CCTV alloc]init];
        //mytv 的初始化方法中 已经向通知中心订阅了 名字为 cctv 的通知
        self.mytv = [[MyTv alloc]init];
    }
    
    成功的三大原则: 1、坚持 2、不要脸 3、坚持不要脸
  • 相关阅读:
    一些问题
    为什么Python在列表,元组和字典的末尾允许使用逗号?
    #!/bin/bash
    gitbook 入门教程之小白都能看懂的 Gitbook 插件开发全流程
    go 学习笔记之10 分钟简要理解 go 语言闭包技术
    gitbook 入门教程之还在搞公众号互推涨粉?gitbook 集成导流工具,轻轻松松躺增粉丝!
    go 学习笔记之仅仅需要一个示例就能讲清楚什么闭包
    go 学习笔记之学习函数式编程前不要忘了函数基础
    go 学习笔记之无心插柳柳成荫的接口和无为而治的空接口
    go 学习笔记之万万没想到宠物店竟然催生出面向接口编程?
  • 原文地址:https://www.cnblogs.com/xulinmei/p/7420179.html
Copyright © 2011-2022 走看看