OC-通知&Size Classes

1.Size Classes

  • 是什么
    为了适配越来越多的不同尺寸的屏幕
  • 核心理念
    将9块屏幕,进行分类,然后设计界面时,针对这一类别,进行设计,然后,等到程序运行时,系统会根据当前实际设备,判断出当前屏幕属于那一个类别,就加载针对这一类别设计的那个界面

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

  • 作用: 完成对象之间的消息传递
  • 核心理念

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

  • 系统提供的通知
    • 键盘通知
    • 每当键盘弹起时系统会自动发出名字叫
    • UIKeyboardWillShowNotification
    • UIKeyboardDidShowNotification
    • 每当键盘收起时系统会自动发出名字叫
    • UIKeyboardWillHideNotification
    • UIKeyboardDidHideNotification
    //1.获取通知中心
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    //2.发通知
    //name     通知的名字
    //object   谁发的通知
    //userInfo  通知的内容
    [center postNotificationName:@"cctv2" object:self userInfo:@{@"time":@"6:20", @"name":@"明天xxx"}];
-(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"]);
}

键盘通知

-(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(@"键盘将要收起");
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值