通知机制

通知的发布:

通知中心:NSNotificationCenter
每一个应用程序都有一个通知中心对象,程序一启动就会有一个通知中心对象。专门负责不同对象之间的消息传递

一个完整的通知一般包含3个属性
-(NSString *)name;    //通知的名称
-(id)object;         //通知的发布者
-(NSDictionary *)userInfo;         //通知发布者传递给通知接收者的信息内容


//监听通知一定要在发布通知之前
[[NSNotificationCenter defaultCenter] addObserver:p1 selector:@selector(getNew) name:@"军事新闻" object:com1];  //只要发布了军事新闻的通知,就会调用p1的getNew方法

NSNotification *note = [NSNotification notificationWithName:@"军事新闻" object:com1 userInfo:@{@"title": @"xxxxx"}];
[[NSNotificationCenter defaultCenter] postNotification:note];


//还可以传递参数


[[NSNotificationCenter defaultCenter] addObserver:p1 selector:@selector(getNew:) name:@"军事新闻" object:com1];  //只要发布了军事新闻的通知,就会调用p1的getNew方法    加一个冒号就会拿到整个通知对象

NSNotification *note = [NSNotification notificationWithName:@"军事新闻" object:com1 userInfo:@{@"title": @"xxxxx"}];
[[NSNotificationCenter defaultCenter] postNotification:note];

//发布通知方法2
[[NSNotificationCenter defaultCenter] postNotificationName:@"军事新闻" object:com1 userInfo: @{@"title":@"xxxxxx"}];
//匿名通知,
[[NSNotificationCenter defaultCenter] postNotificationName:@"军事新闻" object:nil userInfo: @{@"title":@"xxxxxx"}];

通知移除:

//当对象被销毁之前移除通知,移除监听器
[[NSNotification defaultCenter] removeObserver:p1 name:@"军事新闻" object:com1];

UIDevice类

它代表着设备,单例对象,通过它可以进行适配

double version = [UIDevice currentDevice].systemVersion.doubleValue;

UIDeviceOrientationDidChangeNotification   // 设备旋转
UIDeviceBatteryStateDidChangeNotification  //电池状态改变
UIDeviceBatteryLevelDidChangeNotification  //电池电量改变


键盘通知

我们经常需要在键盘弹出或隐藏的时候做一些事情,因此需要监听键盘的状态
键盘状态改变的时候,系统会发出一些特定的通知
UIKeyboardWillShowNotification  //键盘即将显示
UIKeyboardDidShowNotification   //键盘显示完毕
UIKeyboardWillMidNotification   //键盘即将隐藏
UIKeyboardDidHiddenNotification  //键盘隐藏完毕
UIKeyboardDidChangeFrameNotification   //键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification   //键盘的位置尺寸改变完毕





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值