通知的发布:
通知中心: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 //键盘的位置尺寸改变完毕